Java programmer must read: Basic (5)

Source: Internet
Author: User
Tags object copy execution instance method variables return variable access
Program | programmer Java programmer must read: Basic articlesTime: 2001/09/13 13:31 Author: ZSC Pacific Network College

 
2.4 Instance and class members

2.4.1 Understanding instances and class members

The following is a detailed discussion of examples and class members, involving variables and methods as well as class variables and methods:

You declare a member variable in such a way that there is a float type of afloat in the class MyClass:

Class MyClass {

float afloat;

}

So you declare an instance variable. Each time you create an instance of a class, the system creates a copy of each instance variable of the class for the instance. You can access the object's instance variables from the object.

The instance variable is not the same as the class variable, and the class variable is declared using the amount of static modification. Regardless of how many instances the class creates, the system assigns class variables to each class variable. The memory that the system allocates for a class variable occurs when it first invokes the class. All instances share the same copy of class variables for the class. You can access class variables either through an instance or through the class itself.

Their approach is similar: your class can have instance methods and class methods. An instance method operates on an instance variable of the current object and accesses the class variable. Alternatively, the class method cannot access the instance variables defined in the class unless they create a new object and access them through the object. Similarly, class methods can be invoked in a class, and you do not need an instance to invoke a class method.

By default, a member of a definition in a class is an instance member unless the other member is specified. The class defined below has an instance variable with an integer x, two instance methods X and Setx, which set the other objects and the value of the query X.

Class Anintegernamedx {

int x;

public int x () {

return x;

}

public void SetX (int newx) {

x = newx;

}

}

Each time you instantiate a new object from a class, you can get a copy of the instance variable for each class. These replicas are related to the new object. So, each time you instantiate a new Anintegernamedx object from this class, you get a new copy of the new Anintegernamedx object.

All instances of a class share the same practice of an instance method; All Anintegernamedx instances share the same execution of X and Setx. Note here that two methods X and Setx refer to an object's instance variable x. But, you might ask: if all Anintergernamedx share x and setx the same execution, will it create ambiguity? The answer is, of course: No. In an instance method, the name of an instance variable refers to an instance variable of the current object, if the instance variable is not hidden by a method parameter. So in X and Setx, X is equivalent to this x, without causing confusion.
For an object outside Anintegernamedx, if you want to access x, it must be implemented through a specific Anintegernamedx instance. If the code fragment is in the other object's method. It creates two different types of anintegernamedx, it sets X to different values, and then displays them:

Anintegernamedx MYX = new Anintegernamedx ();

Anintegernamedx Anotherx = new Anintegernamedx ();

Myx.setx (1);

anotherx.x = 2;

System.out.println ("myx.x =" + myx.x ());

System.out.println ("anotherx.x =" + anotherx.x ());

Note here that the code uses SETX to set the value of X for MYX, and directly assigns a value to anotherx.x. Regardless of the method, the code operates on two different x replicas: one contained within the MYX object and the other contained in the Anotherx object. The output is implemented with the following code fragment:

myx.x = 1

anotherx.x = 2

The code above shows that each instance of a class Anintegernamedx has its own copy of the instance variable x and each x has its own value.

When declaring a member variable, you can specify that the variable is a class variable rather than an instance variable. Similarly, you can specify that the method is a class method rather than an instance method. The system creates a copy of a class variable the first time the class is called to define the variable. All class instances share the same copy of the class variable. Class methods can manipulate only the class variables, and they cannot access the instance variables defined in the class.

To specify that a member variable is a class variable, you can use the static keyword. For example, we can modify the Anintegernamedx class above so that the x variable is now a class variable:

Class Anintegernamedx {

static int x;

public int x () {

return x;

}

public void SetX (int newx) {

x = newx;

}

}

Now set their x values and display different outputs:

myx.x = 2

anotherx.x = 2

This time the output is different because X is now a class variable, so there is only a copy of this variable, which is shared by all instances of Anintegernamedx, including Myx and Anotherx. When you call Setx in other instances, you can change the value of x for all instances of Anintegernamedx.

Similarly, when we declare a method, you can specify that the method is a class method rather than an instance method. A class method can only operate in a class variable and cannot access all instance variables defined in the class.

To specify a method as a class method, you can use the static keyword at the method declaration. Next, we modify the Anintegernamedx class again so that its member variable x is an instance variable, and its two methods are class methods:

Class Anintegernamedx {

int x;

static public int x () {

return x;

}

static public void SetX (int newx) {

x = newx;

}

}

When you want to compile this version of Anintegernamedx, the compiler will display the following error: Anintegernamedx.java:4: Can ' t make a static reference to

nonstatic variable X in class Anintegernamedx.

return x;

^

These errors occur because the class method cannot access the instance variable unless the method first creates an instance of the Anintegernamedx and accesses the variable through it.

Let's revise the Anintegernamedx to make the x variable a class variable:

Class Anintegernamedx {

static int x;

static public int x () {

return x;

}

static public void SetX (int newx) {

x = newx;

}

}

Now set the value for X and print out the x values:

myx.x = 2

anotherx.x = 2

Again, we change x by MYX and change it to another instance of Anintegernamedx.

Another difference between instance members and class members is that class members can be accessed from the class itself. You do not have to instantiate a class to access its class members. Let's write a piece of code to access X and setx directly from the Anintegernamedx class:

. . .

Anintegernamedx.setx (1);

System.out.println ("anintegernamedx.x =" + anintegernamedx.x ());

. . .

It is worth mentioning that you do not have to create MYX and Anotherx now. You can set X and directly Anintegernamedx the class to retrieve X. You cannot use instance members to handle it, you can invoke an instance method from only one object and access the instance variable only from the object. You can access class variables and methods from instances of classes or from classes themselves.



On

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.