2 / 0 classes and objects, member properties and member methods

Definition of classes and objects

Class definition

[Access modifier] class Class name {  
[private/protected/public]   Declaration and definition of members ;


  • Class is the keyword for declaring a class, and < class name > is an identifier, usually starting with an uppercase letter, which is used to distinguish it from object names, methods and variable names.
  • The class body can contain member properties and member methods, and the member property declaration must end with a semicolon.
  • Defining a class is equivalent to defining a type and a scope.

Access modifier

  • The private object is accessed by the get set method
  • protected
  • Public public

Object instantiation

  • Class is only an abstract data type, and class object is a concrete operable entity. Use the functions provided by the object class.
<Class name>  <Object name> = new <Class name> ([Parameter 1, parameter 2,...]) ;
<Class name>  <Object name>;
<Object name> = new <Class name> ([Parameter 1, parameter 2,...]) ;

Student mary = new Student(18,"male");
Student mary;
mary = Student(18,"male");

Default value

Local variables have no initial value, which is different from member properties
Member properties have default values

boolean -> false
byte short int ->0
long ->0L
Reference type - > null

Access to class members

General forms of accessing properties:
<Object name>.<Attribute name>;
General form of access method:
<Object name>.<Method name>([<Parameter 1>,<Parameter 2>,...]);

private privatized members can only pass parameters through public set methods and get methods.

Special access form: POJO class

Name: PoJo class
Features: methods and attributes appear in pairs, private attributes and public set and get methods.
Function: bearing data

public class Hanshu {
	  private String brand;
	  private int prince;//Member properties
	  //Member method (local variables are defined in the method)
	  public void setBrand(String brand) {
		  this.brand = brand;
	  public String getBrand() {
		  return brand;
	  public void setPrince(int prince) {
		  this.prince = prince;
	  public int getPrince() {
		  return prince;

Member of class

In Java, methods can only be defined in classes, which are called class member methods. The basic syntax is:
[method modifier] method return value type method name ([< parameter list >]){
Method body;

Member method

Method modifier

  • The private object is accessed by the get set method
  • protect
  • Static static method - > use static methods in static method - > or instantiate the class of the method you want to use as an object
  • final constants or methods cannot be overridden, and classes cannot be inherited
  • Abstract method declared in advance - > represents an abstract method, which is not specific.
  • synchronize multithreading

Method transmission parameter

pass by value

When the parameter is of basic data type, the value will not be changed

Address delivery

When the parameter is a reference type: class, interface, array, enumeration, the reference value is passed


  • When the parameter is a basic data type, the method cannot modify the value of the actual parameter.
  • When the parameter is a reference type, the method changes the content of the actual parameter object through address reference

Method overloading

Overload condition

  • In the same class
  • Same method name
  • Different parameter types

Overload advantage

  • Method calls to different parameters become simple
  • Whether it is static does not serve as the basis for overloading
  • Overloaded methods call each other
  • The return value can be different

Special member method - construction method

Construction method: the method called when instantiating a class into an object. There is no return value, and the method name is the same as the class name. It is called automatically when creating an object using new
, and only called once.
1. If a construction method with parameters is defined, the system will not automatically add a construction method without parameters.
2. If no construction method is defined, the system will automatically construct a parameterless construction method for the class.

this keyword

  • This is used to reference properties and methods in this class
    • This. Property, this. Method ()
  • This calls the constructor of this class
    • Use the form of this(), this (parameter)
    • this calls the constructor and can only be placed on the first line of the overloaded constructor
  • this can also be used as a return value to return the reference of the object of the current calling method.
public class Count {
       private int num;
       public Count add(){
              return this;
       public int getNum() {
              return num;
       public static void main(String[] args) {
              Count cou = new Count();

The output value is 2. The method was called twice.

Garbage collection mechanism (easy for interview)

In C program

  • You can manually clean or delete the data in the cache and clean it through free.

Power shortage of manual memory management

  • If there are errors or defects in the program, it will lead to memory leakage.
  • Writing code for thorough manual memory management is an important and complex task, so it will double the development workload of complex programs.

Java garbage collector

  • Java's "garbage collector" provides an automatic solution for memory management.

  • The garbage collector can automatically release memory resources that are no longer used in the program.

  • Disadvantages of automatic garbage collection

    • There is no complete control over when the "garbage collector" executes or does not execute. (in Java programs, you can request the JVM to run the garbage collector, but there is no guarantee that the JVM will agree to the request
  • When Java feels out of memory, it runs the garbage collector

  • When no reference can access an object, the object is suitable for garbage collection.

**Garbage collection scenario 1: empty reference**

public class CatTest{
public static void main(String[] args){
     Cat c1;
     Cat c2;
     c1 = new Cat(""Floret");//Create objects and references
     c2 = c1;
     c1 = new Cat(""Xiaobai");

Finally, because there is no reference to "Xiaohua", it is an object that has no reference to access and can be garbage collected.

Garbage collection scenario 2: isolated references

public class Island{
  Island n;
  public static void main(String[] args){
        Island i2 = new Island(); 
        Island i3 = new Island(); 
        Island i4 = new Island();
        i2 = null;
        i3 = null;
        i4 = null;

The pointer parts of i1, i2 and i3 refer to each other, but no reference can access them.

Keywords: Java C#

Added by heropage on Sat, 25 Sep 2021 04:24:50 +0300