Classes in Java

Source: Internet
Author: User
Tags class definition

Class can be seen as a template for creating Java objects.

Use the following simple class to understand the definition of a class in Java:

publicclassdog{

Stringbreed;

Intage

Stringcolor;

Voidbarking () {

}

Voidhungry () {

}

Voidsleeping () {

}

}

A class can contain the following types of variables:

Local variables: Variables defined in a method, construction method, or block of 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 within 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 multiple methods, in the example above: Barking (), hungry (), and sleeping () are all methods of the dog class.

Construction method

Each class has a constructor method. If you do not explicitly define a construction method for a class, the Java compiler will provide a default constructor for the class.

At least one constructor method is called when an object is created. The name of the constructed method must be the same as the class, and a class can have more than one constructor method.

Here is an example of a construction method:

publicclasspuppy{

Publicpuppy () {

}

Publicpuppy (Stringname) {

This constructor has only one parameter: name

}

}

Creating objects

Objects are created from a class. In Java, use the keyword new to create a new object. Creating an object requires the following three steps:

Declaration: Declares an object, including the object name and object type.

Instantiation: Use the keyword new to create an object.

Initialize: When you create an object with new, the constructor method is called to initialize the object.

Here is an example of creating an object:

publicclasspuppy{

Publicpuppy (Stringname) {

This constructor has only one parameter: name

System.out.println ("Puppy's name is:" + name);

}

Publicstaticvoidmain (String[]args) {

The following statement creates a puppy object

Puppymypuppy = Newpuppy ("Tommy");

}

}

Compiling and running the above program will print out the following results:

The puppy's name is: Tommy

Accessing instance variables and methods

Access member variables and member methods through the objects you have created, as follows:

/* Instantiate Object */

Objectreference = Newconstructor ();

/* Access Variables in it */

Objectreference.variablename;

/* Access the methods in the class */

Objectreference.methodname ();

Instance

The following example shows how to access an instance variable and invoke a member method:

publicclasspuppy{

Intpuppyage;

Publicpuppy (Stringname) {

This constructor has only one parameter: name

System.out.println ("Puppy's name is:" + name);

}

Publicvoidsetage (intage) {

Puppyage = age;

}

Publicintgetage () {

System.out.println ("Puppy Age:" + puppyage);

Returnpuppyage;

}

Publicstaticvoidmain (String[]args) {

/* Create Object */

Puppymypuppy = Newpuppy ("Tommy");

/* Use the method to set age */

Mypuppy.setage (2);

/* Call another method to get the age */

Mypuppy.getage ();

/* You can also access the member variable as follows */

SYSTEM.OUT.PRINTLN ("Variable value:" + mypuppy.puppyage);

}

}

Compile and run the above program to produce the following results:

The puppy's name is: Tommy

The puppy's age is: 2

Variable Value: 2

Source file claim rules

In the final part of this section, we will learn the claim rules for the source file. When you define multiple classes in one source file, and you also have import statements and package statements, pay particular attention to these rules.

There can be only one public class in a source file. A source file can have multiple non-public classes. The name of the source file should be consistent with the class name of the public class. For example: The class name of the public class in the source file is employee, then the source file should be named Employee.java.

If a class is defined in a package, then the packages statement should be in the first row of the source file.

If the source file contains an import statement, it should be placed between the package statement and the class definition. If there is no package statement, the import statement should be first in the source file.

The import statement and the package statement are valid for all classes defined in the source file. In the same source file, you cannot give different package declarations to different classes.

The class has several access levels, and the classes are divided into different types: abstract and final classes, and so on. These will be described in the Access Control section.

In addition to some of the types mentioned above, Java also has some special classes, such as: Inner class, anonymous class. If you do not know the friend can add me Q, or add group number to study together, we learn to share video programming, hope to help like Java friends. If you need any help, you can contact me.

Classes in Java

Related Article

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.