Java Objects and classes

Source: Internet
Author: User
Tags class definition

 Public class dog{   String breed;    int Age   ;   String  color;        void Barking () {    }        void  hungry () {      }   void  sleeping (){         }}                 

A class can contain the following types of variables:

1, 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.

2, member variable: A member variable is defined in a class, a variable outside the method body, a variable that is instantiated in a created object , a member variable can be a method in a class,

Constructs a method and a statement block access to a specific class of statements.

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

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:

 Public class puppy{    public  Puppy () {    }     public  Puppy (String name) {         //  This constructor has only one parameter: name    }}

To create an object:

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 .
public class puppy{public   Puppy (String name) {      //This constructor has only one parameter: name      System.out.println ("The puppy's name is:" + name); c3/>} public   static void main (String []args) {      ///The following statement will create a Puppy object      Puppy mypuppy = new Puppy ("Tommy");   }}

To access instance variables and methods:

 Public classpuppy{intPuppyage;  PublicPuppy (String name) {//This constructor has only one parameter: nameSystem.out.println ("Puppy's name is:" +name); }     Public voidSetage (intAge ) {Puppyage=Age ; }     Public intGetage () {System.out.println ("Puppy's Age:" +puppyage); returnPuppyage; }     Public Static voidMain (String []args) {/*Creating Objects*/Puppy mypuppy=NewPuppy ("Tommy" ); /*use the method to set age*/Mypuppy.setage (2 ); /*call another method to get the age*/Mypuppy.getage (); /*You can also access member variables as follows*/System.out.println ("Variable Value:" +mypuppy.puppyage); }}

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.

Java Objects and classes

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.