1, Creation of classes and objects
A healthy program should only contain a main method as the entry of the program. Usually, we define the class containing the main method as Application.
public class Application { public static void main(String[] args) { } }
1. Class creation is very simple. It only contains attributes and methods. Here we create a Student class.
//Student class public class Student { //attribute String name; int age; //method public void study(){ //Keyword this calls a property in the current class System.out.println(this.name+"I am learning"); } }
2. Use the new keyword to create an object
- When creating an object with keywords, in addition to allocating memory space, the created object will be initialized by default and the constructor in the class will be called.
//A project should have only one main method as the entry for program execution public class Application { public static void main(String[] args) { //Instantiate object Student xiaoMing = new Student(); Student xiaoHong = new Student(); xiaoMing.name = "Xiao Ming"; xiaoMing.age = 10; System.out.println(xiaoMing.name); System.out.println(xiaoMing.age); xiaoMing.study(); System.out.println("========================="); xiaoHong.name = "Xiao Hong"; xiaoHong.age = 12; System.out.println(xiaoHong.name); System.out.println(xiaoHong.age); } }
3.static, directly call the methods in the class.
Remember the role of static?
It allows us to call the methods of the current class directly in the class. In fact, it also allows us to use methods in other classes (these methods have the modifier static).
It is also a Student class
public class Student { String name; int age; public static void sayHello(){ System.out.println("hello"); } }
At this point, Application
public class Application { public static void main(String[] args) { Student.sayHello(); } }
Run it and see the effect of this program.
2, Constructor
- Constructors must master.
- Even if a class doesn't write anything, it will have a method, which is a constructor.
- Constructors, also known as construction methods, must be called when creating objects.
- It is troublesome to define constructors manually, so if there are no special requirements for constructors, you can directly create classes, which will automatically generate a default constructor (parameterless construction).
Define a Person class and manually define the constructor (the same as the default constructor)
public class Person { String name; //default constructor public Person() { } }
At this point, Application
public class Application { public static void main(String[] args) { //Instantiate object Person person = new Person(); System.out.println(person.name); //null } }
The constructor has the following two characteristics:
- Must be the same as the class name
- There is no return type, and void cannot be written
An important role of the constructor is instance initialization. When instantiating, the default constructor initializes the attribute to the initial value of the corresponding type. We can define the constructor to realize the user-defined initial value.
Modify the Person class and customize the constructor
public class Person { String name; public Person() { this.name = "zhangsan"; } }
At this point, Application
public class Application { public static void main(String[] args) { //instantiation Person person = new Person(); System.out.println(person.name); //zhangsan } }
In addition to parameterless construction, we can also use method overloading to define parameterless construction.
Modify the Person class
public class Person { String name; //default constructor // public Person() { // } //Nonparametric structure public Person(){ this.name = "zhangsan"; } //Parametric structure public Person(String name){ this.name = name; } }
At this point, Application
public class Application { public static void main(String[] args) { //Instantiate two objects Person person1 = new Person(); Person person2 = new Person("Zhang San"); System.out.println(person1.name); //zhangsan System.out.println(person2.name); //Zhang San } }