In-depth analysis of the application of Java class attributes and class methods

Java class attributes

Java class attributes, also known as fields, are variables in a class. They are used to store class-related data.

Create class attributes

Declare properties in class definition:

public  class  Main {
   int x; // attribute 
  String name; // attribute 
}

Access class properties

Access the properties of an object using dot syntax:

Main  myObj  =  new  Main ();
myObj.x = 5 ; //Set the property value 
System.out.println(myObj.x); //Get the property value

Modify class attributes

The property values ​​of an object can be modified:

Main  myObj  =  new  Main ();
myObj.x = 5 ;
myObj.x = 10 ; // Modify property value 
System.out.println(myObj.x); // Output 10

Property type

Properties can be of any data type, including:

  • Basic types: int, double, boolean, char, etc.
  • Reference types: String, Date, List, etc.

modifier

Modifiers can be used to control access to properties:

  • public: public access
  • private: private access
  • protected: protected access
  • default: default access

Example

public  class  Main {
   private  int x; // Private properties 
  public String name; // Public properties

  public  void  myMethod () {
     // Can access private properties 
    x = 10 ;
  }

  public  static  void  main (String[] args) {
     Main  myObj  =  new  Main ();
     // Can access public properties 
    myObj.name = "John Doe" ;
     // Cannot access private properties 
    // myObj.x = 5; // mistake
  }
}

Some additional notes:

  • In Java, class properties are usually defined as private so that they can only be accessed through the class’s methods.
  • For convenience, class properties can also be defined as public, but this makes them more susceptible to accidental changes.
  • It is recommended to use getter and setter methods to access and modify class properties to provide better control over access to properties.

Example:

public  class  Main {
   private  int x;

  public  int  getX () {
     return x;
  }

  public  void  setX ( int x) {
     this .x = x;
  }

  public  static  void  main (String[] args) {
     Main  myObj  =  new  Main ();
    myObj.setX( 5 );
    System.out.println(myObj.getX()); // Output 5
  }
}

In this example, the x property is private, but can be accessed and modified through the getX() and setX() methods.

There are some other things related to class properties:

  • Static properties: Static properties belong to the class itself, not to instances of the class.
  • Constant attributes: The value of a constant attribute cannot be modified.
  • Enumerated properties: The value of an enumerated property can only be one of a predefined set of values.

Java class methods

Java class methods are blocks of code declared within a class to perform specific operations. They are similar to functions, but are associated with the class itself rather than with instances of the class.

Create class method

Use staticthe keyword to declare a class method:

public  class  Main {
   static  void  myMethod () {
    System.out.println( "Hello World!" );
  }
}

Call class method

Call a class method using the class name and method name, followed by parentheses ():

public  class  Main {
   static  void  myMethod () {
    System.out.println( "Hello World!" );
  }

  public  static  void  main (String[] args) {
    myMethod();
  }
}

Example:

public  class  Main {
   static  void  myMethod (String name) {
    System.out.println( "Hello, " + name + "!" );
  }

  public  static  void  main (String[] args) {
    myMethod( "John Doe" );
  }
}

Output:

Hello, John Doe!

Class methods and instance methods

  • Class methods belong to the class itself, while instance methods belong to instances of the class.
  • Class methods can be called directly through the class name, while instance methods need to be called through an instance of the class.
  • Class methods are typically used to perform common operations related to a class, while instance methods are typically used to operate on instances of a class.

modifier

Modifiers can be used to control access to class methods:

  • public:Public access
  • private:private access
  • protected: protected access
  • default:Default access

Example:

public  class  Main {
   private  static  void  myMethod () {
    System.out.println( "Hello World!" );
  }

  public  static  void  main (String[] args) {
     // myMethod(); // Error, private method cannot be accessed
  }
}

Some additional notes:

  • Class methods are typically used to perform common operations related to the class, such as:
    • Create new instance
    • Validate input
    • Provide tool class methods
  • Instance methods are typically used to manipulate instances of a class, for example:
    • Get or set property value
    • perform calculations
    • Change the state of an object
  • You can use finalthe keyword to declare a class method so that it cannot be overridden.
  • Abstract class methods can be declared using abstractthe keyword, and their definition must be provided by the subclass.