[JAVA] Relationship between package and class

What is a class

A class is simply a "set of fields and methods". (In terms of mathematics, "field" is close to a variable or constant, and "method" is close to a function.)

class Sample01{
//field
  int a = 0;
  int b;
  
//Method
  int sum(int c, int d){
    return c+d;
  }  
  int add_a(int c){
    return a+c;
  }
}

You can see it in the sample above. Like the second method, fields in the same class can appear inside the method. Note that if the argument is int a here, the one entered as an argument will be distinguished from "a" and the field will be distinguished from "this.a".

What is a package?

A package is a class or set of packages. The relationship between classes and packages is similar to the relationship between files and folders. You can access fields and methods in classes in the same package using the access modifier (.).

class Sample02{
  int a = Sample01.a;
  int b = 2;
  
  public void main(String[] args){
    int c = Sample01.sum(a,b);
    System.out.println("c = " + c);
  }
}

Running the above code should give you "c = 3". So how do you access fields, etc. in a class in another package? In such a case, use "import".

import package name.name of the class;

If you declare, you will be able to access the fields and methods of that class using the same procedure as before.

Recommended Posts

Relationship between package and class
Relationship between Controller and View
Difference between class and instance
Difference between interface and abstract class
Relationship between ActiveRecord with_lock and cache
Relationship between database and model (basic)
[Java] Relationship between H2DB and JDBC
Differences between Applet class and JApplet class
Class and model
Relationship between kotlin and java access modifiers
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
[Java] Introductory structure Class definition Relationship between class and instance Method definition format
[Java] Differences between instance variables and class variables
Relationship between Eclipse m2e plugin and Maven
Ruby: Differences between class methods and instance methods, class variables and instance variables
Relationship between UI test and recording, implementation method
abstract (abstract class) and interface (interface)
Switch between JDK 7 and JDK 8
Class and instant part2
Difference between vh and%
Difference between i ++ and ++ i
What is the difference between a class and a struct? ?? ??
Difference between Spring AOP and library proxy target class
Verification of the relationship between Docker images and containers
Difference between product and variant
java.util.Optional class and null and not null
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Java class definition and instantiation
Difference between variables and instance variables
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Differences between IndexOutOfBoundsException and ArrayIndexOutOfBoundsException
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
[Ruby] Maybe you don't really understand? [Difference between class and module]
The relationship between strict Java date checking and daylight savings time
About the relationship between the Java String equality operator (==) and initialization. Beginners
Easy to understand the difference between Ruby instance method and class method.
[Ruby] Difference between get and post
Difference between render method and redirect_to
Difference between == operator and equals method
Relationship between Homebrew + rbenv + ruby-build (Part 1)
Mutual conversion between Function and Consumer
Differences between "beginner" Java and Kotlin
Get TypeElement and TypeMirror from Class
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
[Rails] Difference between find and find_by
StringBuffer and StringBuilder Class in Java
[JAVA] Difference between abstract and interface