Java Fundamentals 2

Source: Internet
Author: User

1: Why the static method cannot access the instance variable of the class

Because they don't have a place with Ben.
Static methods are stored in the stack, while other non-static methods are stored in the heap
Basic
Stack | Heap
static | Non-static
Does not belong to a system at all ....

2: Pass by reference

Importjava.awt.Dimension; Public classReferencetest { Public Static voidMain (string[] args) {Dimension d=NewDimension (5,10); Referencetest RT=Newreferencetest (); System.out.println ("Before modify d.height=" +d.height);        Rt.modify (d); System.out.println ("After modify d.height=" +d.height); }    voidModify (Dimension Dim) {Dim.height=dim.height+1; System.out.println ("Dim.height=" +dim.height); }}

Output:

Before modify d.height=10
dim.height=11
After modify d.height=11

Reference variables D and Dim all point to the same object. When you pass an object variable to a method, you must remember that the object reference is passed instead of the actual object itself. The actual reference variable is not passed at all, but a copy of the reference variable. A copy of a variable means that a bit copy within that variable is obtained, so when a reference variable is passed, it is passed a bit copy that represents how to get a particular object. In other words, both the calling method and the called method now have identical copies of the reference, so both refer to the exact same object on the heap (this is not a copy).

3: Pass by value.

For all variables running within a single JVM, Java is actually passed by value. Passing by value means passing by the value of a variable, that is, passing a copy of the variable.

  

Java Fundamentals 2

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.