Saturday 15 May 2010

Java keyword in front of new declaration -


I'm trying to learn some Java.

When I run the code below, it works:

/ * * To change this license header, select the license header in the project properties. * To change this template file, select the tool. Templates * and open the template in the editor. * / Package testing; / ** * * @ Author Mai * / Public Class Test {int myAge; Public getAge () {system.out.println (myAge); } / ** * @complete command line argument * / public static zero main (string [] args) {// TODO code application logic here test tester = new test (); Tester.myAge = 5; Println (Tester.myAge); }}

As you can see that my main function is a test tester. When I remove it equally:

  tester = new test ();  

It does not work anymore. Why should I add a test before this function. Remember, I'm still very passionate about Java programming (this is my first program), so be gentle.

Java works in a way that

  typename var_name = New TypeConstructor ();  

Here, TypeName is the name of your class. However, you can do this in two parts:

  TypeName var_name; // initially var_name = new type constructor (); // or by using another method that returns the object of TypeN  

However, you need to make sure that it is either an institution or be assigned correctly. Without TypeName, Java can not solve new type constructor () syntax as you did.

An important thing is that the TypeConstructor should be of the same type as TypeName and var_name . You can not do the following like

  string var_name; // I declared var_name string var_name = new tester (); // I have the keys to the var_name typifier type default - Failure  

"Instantiation" and "declaration" are the keys here - you can simply "uninstalled" variable var_name And java will not scream. See examples at This is like a job contract that you sign with your employer (T & C applies). Once you sign it (test declare tester ), you can not just go back and say that "I type test type type tester" (i.e. test = new tester () ). You are breaking the T & C contract, as long as you do not arrange for a new contract.


No comments:

Post a Comment