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).
Answer from Rika on Stack OverflowWhat are Keywords in Java? - Stack Overflow
Need a detailed understanding of the keywords in the beginning of any java programming.
Add or modify keywords in java language - Stack Overflow
Java Keywords
Videos
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).
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
I understand that all programming need to start with
public class Test {
public static void main(String[] args){
}
}
But after searching and reading many articles and watching videos I still don't understand why we need static, void, String[] args, and why when we change void to a primitive or reference type we need to have a return 0;. why don't we need that when its a void?
Every book I read, class I attended, or video I've watched, none ever fulfilled my understanding of those things and I cant move forward when I don't understand what is happening from the beginning.
One approach that uses a rather sophisticated toolchain and could be considered as an "overkill", but is not as much effort as writing an own compiler or so:
- Download ANTLR4 from http://www.antlr.org/download.html
- Download the Java Grammar at https://github.com/antlr/grammars-v4/blob/master/java/Java.g4
- Modify the Java Grammar according to your needs...
Run
java -classpath "antlr-4.4-complete.jar" org.antlr.v4.Tool Java.g4This will generate some files, one of them being
JavaLexer.java.- Create a Java Project that contains the ANTLR JAR and the
JavaLexer.java Create a class like the following, which does the translation:
import java.io.IOException; import org.antlr.v4.runtime.ANTLRFileStream; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.TokenStream; public class Main { public static void main(String[] args) throws IOException { CharStream s = new ANTLRFileStream("Input.javaX"); JavaLexer lexer = new JavaLexer(s); TokenStream t = new CommonTokenStream(lexer); int i = 1; while (true) { if (t.LA(i) == -1) { break; } if (t.LA(i) == JavaLexer.IMPORT) { System.out.print("import "); } else { System.out.print(t.LT(i).getText() + " "); } i++; } } }(of course, this is only an example that only translates the
IMPORTtoken, which was defined in the grammar file to be"uvoziti". For a more general and flexible translation, one would define the translation in an external file, and probably read this file to create a mapMap<Integer, String>that mapsJavaLexer.IMPORTto"import"etc...)Create the input file from the example:
Input.javaX:uvoziti java.io.File; public class Input { public static void main(String args[]) { File file = null; System.out.println("done"); } }When you then run the
Main, it will read this input file, eventually find theIMPORTtoken, and instead of the original text (uvoziti) it will printimport.The result will be the contents of a Java file, with an awful formatting...
import java . io . File ; public class Input { public static void main ( String args [ ] ) { File file = null ; System . out . println ( "done" ) ; } }but fortuntately, the compiler does not care about the formatting: You may write this output directly to a
.javafile, and the compiler will swallow it.
As it is described here, it is only a proof of concept. For a flexible and generic translation of many (all) keywords, one would have to build some infrastructure around all that. The input files should be read automatically (File.listFiles(), recursively for packages). Each of them would have to be translated (using the Map<Integer, String> that I mentioned earlier). Then the output files would have to be written and compiled, either with the runtime JavaCompiler, or by manually invoking the javac with Runtime#exec.
But in general, I think that this should be doable within a few hours in the best case, and within one week when considering that everything takes longer than you think.
Writing an own Java compiler, on the other hand, might take a bit longer, even when you consider that everything takes longer than you think...
Java doesn't provide any way to redefine keywords.
If you add or remove keywords to the Java language, then it isn't Java anymore.
You could write your own language that compiles to Java. This could be as simple as writing a program that does a string replace of uvoziti for import and then runs it through the javac compiler.