问题
I am curious about keywords in Java.
Should I understand it as a class of various methods that get created with every instance of user-defined classes, like the ultimate “super” class? How is it defined in the Java?
So for example, I came across this:
class A {
class B {}
}
A a = new A();
B b = a.new B();
This seems like each class has keyword new
as its own method. I would appreciate any insights on how keywords are defined/implemented in Java.
回答1:
Classes don't have keyword "new" as a personal method or anything like that. It is the Java language itself that has the keyword "new". So in other words you put "new" in the code the compiler would recognize it and instantiate a new Object.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.9- this link is the documentation of Java language, in section 3.9 it shows all the keywords.
Edit: Like others are saying, what the snippet of code in your question indicates an inner class, so for instance, like it says in http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
public class ShadowTest {
public int x = 0;
class FirstLevel {
public int x = 1;
void methodInFirstLevel(int x) {
System.out.println("x = " + x);
System.out.println("this.x = " + this.x);
System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
}
}
public static void main(String... args) {
ShadowTest st = new ShadowTest();
ShadowTest.FirstLevel fl = st.new FirstLevel();
fl.methodInFirstLevel(23);
}
}
The following is the output of this example:
x = 23
this.x = 1
ShadowTest.this.x = 0
This shows that the innerclass or class B(FirstLevel) is like or similar to the outer class's variables and methods(for it is associated with the instance of the outer class) of class A(ShadowTest).
回答2:
As all the languages ,Java has Keywords. Here the new keyword is used for initialization purposes.here the object of the class A is initialised in the first statement. in second statement the object of class B is initialised with class A's object A
回答3:
Keywords in the simplest words are the predefined words which have some predefined specific meaning and cannot be used for any other purpose
.
Check full list of keywords in Java
Now to elaborate this thing I would take example of two keywords, int
and float
. An int
keyword is used to define an integer variable and that is all what a keyword int
can do. We cannot use it for any other purpose. Similarly, float
keyword can be used to define a float/decimal number variable, nothing else.
int a = 10;
I defined a variable a
of integer type.
int float = 20;
<----- It won't work.
In this example, I am trying to declare an integer type variable with name float
, which is not possible, as float it self is a keyword, so it cannot be used as an identifier.
If we talk about new
keyword, then it is used to create objects of any class or I would more precisely say, new
keyword allocates memory to the objects at run time.
It is NOT like that every class has its own new
keyword. new
is Java's keyword so any Java class can use it.
To clarify,
A a = new A();
B b = a.new B();
In the first statement, we are creating an object of class A and as class B is the nested class of class A, so we cannot access it directly. So to access it, we are using the object of class A, which we already have built.
来源:https://stackoverflow.com/questions/26194289/what-are-keywords-in-java