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