Recent use of the time to look at the thought in Java feel again and see the first time look very different
Next, let's talk about the relationship between objects and references in Java, and the concept of inner classes.
1. Everything in Java is an object
What is the operator object in Java? The answer is a reference, which is like a pointer in C or C + +.
If you use a reference, then you have to associate it with an object, otherwise the reference will not be controlled as you think, such as you create a string reference:
This is not associated with any object at this time, if you do something at this point, such as calling some methods of string, there must be a problem (except for some basic types, which, as you define them, will give you the initial value), so the money must be associated with the object:
string s = new string ();
Or
String s = "My name is Ajun";
Just like that.
2, how to associate with the object
in Java, you typically create an object by using new to associate it with a reference, such as:
string s = new String ("My name is Ajun");
This not only creates an object and reference s to associate, but also initializes the colleague, and we can create our own object types.
3, storage location
(1) Stack: General store references and base type variables, which are mainly moved up and down through the stack pointer to allocate and release memory. The
base type variable does not fit into new creation because it takes up less memory.
(2) Heap: For storing Java objects, when a program executes new, the heap assigns a space to the object, remembering that the allocation and release of memory by the heap is more time-consuming than the stack's storage and freeing of memory, which is where the base type variable is to exist on the stack, Because the basic type variables are used most frequently, storing and releasing memory frequently, the performance is conceivable when consuming more.
4, Inner class
(1), Inner class basics:
A class inside a Java class becomes an inner class
inner class can be divided into classes that are defined outside the method body, classes that define the method's interior, Static inner classes (defined only outside the method), anonymous inner class
Description:
defines the class outside the method: The
class's member variable (static, non-static) is accessible and, in order to guarantee the member variable of the class that can be referenced correctly, the object of the superclass must be instantiated first. You can instantiate an object of an inner class
access permission can be any, you can see it as a class member variable, so the understanding is much better.
The class defined in the method body;
The member variables of the class (static, non-static) can be accessed, in order to ensure that the correct reference to the class member variables, so you must first instantiate the object of the class, you can instantiate the internal classes of the object
access rights can not have, see him as a method of local variables on it.
static inner class:
can access only static member variables of a class
access permission any
anonymous inner class: the member variable of the
class (Static, Non-static) is accessible and, in order to guarantee the member variables of the class that can be referenced correctly, the object must be instantiated first. You can instantiate an object of an inner class
access rights can not have
(2), the role of the inner class
internal classes can be a good hiding class, the general class does not allow private protect default access rights.
Inner classes can implement multiple inheritance, making up for Java's inability to inherit more than
(3), example
Package com.ajun.test.innerclass.example;
/** * Fruit Content * @author Administrator */public interface Contents {String value ();
} package com.ajun.test.innerclass.example;
/** * Fruit Destination * @author Administrator * * */public interface Destination {//Destination String Readlabel ();
} package com.ajun.test.innerclass.example;
public class Goods {private String des= ' is ruit!! ';
Method external Private class Content implements contents{private String name = "Apple" +des;
@Override public String value () {return name;
}//Method external private class Gdestination implements destination{private String label;
Private Gdestination (String label) {this.label= label;
@Override public String Readlabel () {return label;
}//Anonymous internal class public destination Getdestination (final String label) {return new destination () { @Override Public String Readlabel () {return label;
}
};
Public destination Dest (String s) {return new gdestination (s);
Public Contents content () {return new content (); Public destination Dest2 (String s) {class Gdestination implements destination{private String label
;
Private Gdestination (String label) {this.label= label;
@Override public String Readlabel () {return label;
Return to New Gdestination (s);
}} package com.ajun.test.innerclass.example;
public class Test {public static void main (String [] a) {Goods GS = new Goods ();
Contents C = gs.content ();
Destination D = gs.dest ("Beijing");
System.out.println (C.value ());
System.out.println (D.readlabel ());
Destination D1 = gs.getdestination ("Shanghai");
System.out.println (D1.readlabel ());
System.out.println (Gs.dest2 ("Tianjin"). Readlabel ());
}
}
The content and gdestination are well hidden, outside of the call, simply do not know what the call is the specific class, so that this class has more inherited characteristics.
Output
Apple is ruit!!
Beijing
Shanghai