concepts of classes and objects:
Object: An object is an instance of a class (objects are not looking for a girlfriend), stateful and behavioral. For example, a dog is an object whose state is: color, name, variety, behavior: wagging the tail, barking, eating, etc.
Class: A class is a template that describes the behavior and state of a class of objects
A class can contain the following types of variables:
Local Variables : variables defined in a method, construction method, or block of statements are called local variables. Both the Declaration and initialization of variables are in the method, and the variables are automatically destroyed when the method ends.
member variables: member variables are variables that are defined in the class, outside the method body. This variable is instantiated when the object is created. Member variables can be accessed by methods, construction methods, and statement blocks of specific classes within a class.
class Variables : Class variables are also declared in the class, outside the method body, but must be declared as static types.
Construction Method:
Each class has a constructor method. If you do not explicitly define a construction method for a class,the Java compiler will provide a default constructor for the class.
At least one constructor method is called when an object is created. The name of the constructed method must be the same as the class, and a class can have more than one constructor method.
Here is an example of a construction method:
public class puppy{
Public Puppy () {
}
Public Puppy (String name) {
This constructor has only one parameter:name
}
}
source file claim rules:
in the final part of this section, we will learn the claim rules for the source file. When you define multiple classes in one source file, and you also have import statements and package statements, pay particular attention to these rules.
there can be only one public class in a source file
A source file can have multiple non- public classes
the name of the source file should be consistent with the class name of the public class. For example: The class name of the public class in the source file is Employee, then the source file should be named Employee.java .
if a class is defined in a package, then the packages statement should be in the first row of the source file.
If the source file contains an import statement, it should be placed between the package statement and the class definition. If there is no package statement, the import statement should be first in the source file.
The import statement and The package statement are valid for all classes defined in the source file. In the same source file, you cannot give different package declarations to different classes.
The class has several access levels, and the classes are divided into different types: abstract and final classes, and so on. These will be described in the Access Control section.
In addition to the several types mentioned above, Java also has some special classes, such as: Inner class, anonymous class.
Java Package:
packages are primarily used to classify classes and interfaces. When developing Java programs, you might write hundreds or thousands of classes, so it is necessary to categorize classes and interfaces.
Import Statement:
in the in Java, if you give a full qualified name, including the package name, the class name, the java compiler can easily navigate to the source code or class. the Import statement is used to provide a reasonable path so that the compiler can find a class.
For example, the following command line will command the compiler to load All classes under the Java_installation/java/io path
Import java.io.*;
Java Objects and classes