Java class and object, Java object

Source: Internet
Author: User

Java class and object, Java object

References: Wang Yinglong Java programming

I. Class syntax

[Modifier] class <class Name> [extends parent class name] [implements interface list] {

// Class body
}

1: Modifier

Optional values:

  • Public: This class can be used by any class;
  • Abstract: This class cannot be instantiated as an abstract class;
  • Final: this class is the final class and cannot be inherited;
  • Omitted: the JAVA compiler will give it a default value, indicating that this class can only be used by other classes in the same package.
2: class <class Name>
  • If this class contains the main method, the name of this Java file must be the same as that of this class;
  • Multiple classes are defined in the same Java file. Its Class modifier can only declare the class containing the main method as public.
3: List of extends parent class names and implements Interfaces
  • A class can inherit non-private members from another class;
  • In Java, only single inheritance is allowed, but in the real world there are multiple relationships. We can implement this multi-relationship through interfaces or internal classes.
4: class body (mainly composed of member variables and member methods) (1) member variables

 [Modifier] [static] [final] <variable type> <variable name>;

 

  • Modifier: (optional) indicates the access permission of the variable. The values are public, protected, and private.
  • Static: Optional. It is used to specify the member variable as a static variable. Static variables do not belong to a certain variable, but belong to the class itself. They are accessed through "class name. variable name. When a static variable is running, the JVM only allocates memory for the static variable once. The static variable memory is allocated during the loading process.
  • Final: optional, indicating that the member variable is a constant.

Member variable/local variable

  • Local variables cannot be declared using the public, protected, private, and static keywords;
  • The function scope is different. member variables are declared in the class and are valid throughout the class. Local variables are variables declared in the method and valid in the method;
  • Member variables do not need to be initialized during definition, but must be initialized.
(2) member Method

[Modifier] <type of the method return value> <Method Name> ([parameter list]) {
// Method body
}

  • The method name cannot be the same as the class name, except the constructor;
  • If multiple methods with the same name appear in the same class, but the return value type or method parameter is different, or the parameter type is different, the method is overloaded ).
  • However, if the number of parameters of a method is the same as that of the type but only the return value type is different in the two or more method definitions, this does not constitute a method overload. At the same time, an error is reported during compilation.
Ii. Constructor
  • A special method. Its name must be the same as the name of its class, and there is no return value, and it does not need to be identified by void.
  • The General constructor is declared as public;
  • Used to create and instantiate objects;
  • The Java compiler automatically provides a constructor without any parameters and is empty. However, if the constructor is explicitly defined in the class, the Java compiler does not provide the default constructor.
  • Multiple constructor methods with different parameters can be defined in a class.
Iii. main Method

Public static void main (String [] args ){}

  • The main method belongs to the class;
  • The String [] args String array parameter is a character that can be obtained from the keyboard.
4. Create and use objects
Class Student {String no; // Student ID String name; // name int age; // age // defines the public void goToClass () {System. out. println (name + "students go to class! "); //" + "Indicates String connection} public class Test {// entry function public static void main (String [] args) {Student stu = new Student (); // instantiate a student object // assign the initial value stu to the object. no = "soft001"; stu. name = "James"; stu. age = 20; stu. goToClass (); // call the class method through an object }}

 

Note: In C/C ++ and other programming languages, you need to manually release the allocated memory after using the allocated memory. However, you do not need to manually release the memory in Java. Java provides the Garbage Collector mechanism to automatically perform these operations (such as the Garbage Collector and finalize () method ); however, it is unknown when the discarded object is destroyed.

V. this reference

This indicates the current object being executed. Another purpose of this is to hide the member variables of the class in the method when the local variables of the method are the same as those of the class member variables. In this case, you can use the "this. class member variable" method to access the object, because this represents the current object. For example:

Public class Student {String name; public Student (String name) {this. name = name;} public static void main (String [] args) {Student stu = new Student ("soft ");}}This keyword instance

 

Multiple constructor methods of a class can also be called. When a constructor needs to call another constructor in this class, you can use the this keyword, and Java requires that this call statement is the first line of code of the entire constructor. For example:

public class Student{    String name;    public Student(String name){        this.name=name;    }    public Student(){        this("soft");    }    public static void main(String []args){        Student stu=new Student();    }}

 

Vi. static keywords

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.