Classes and objects in Java

Source: Internet
Author: User

A class in Java is a template that describes the behavior and state of a class of objects.

An object is an instance of a class in which the object has a State (property) and behavior (method). For example, a dog is an object, his state is his color, name, variety, his behavior is to call, WAG tail, eat and so on.

A class includes the following types of variables:

Local variables: Variables defined in methods and construction methods or in block 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 in a class.

Class variables: Class variables are also declared in the class, outside the method body, but must be declared as static types.

A class can have several methods, such as the dog's name in the example above, wagging its tail.

Construction methods: Each class has a constructor method. At least one constructor method is called when the object is created. The name of the construction method must be the same as the class name, and a class can have multiple construction methods.

Create objects: objects are created from a class. In Java, use the keyword new to create a new object. Trilogy of creating objects: 1 declares an object, including the name of the object and the type 2 instantiation, using the keyword new to create object 3 when you use new to create an object, the constructor method and initialization object are called.

Classes and objects in Java

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.