Refilon
Refilon

Reputation: 3499

Java keyword in front of new declaration

Im trying to learn some Java.

When i run the below code, it works:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

/**
 *
 * @author me
 */
public class Test {

    int myAge;

    public getAge( ){
        system.out.println(myAge);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Test Tester = new Test();
        Tester.myAge = 5;

        System.out.println(Tester.myAge);
    }

}

As you can see there is a Test Tester in my main function. When i remove it like so:

Tester = new Test();

It does not work anymore. Why should i add Test before this function. Remember, I'm still very noob in Java programming (this is my first ever program) so be gentle.

Upvotes: 1

Views: 202

Answers (6)

Kon
Kon

Reputation: 10810

Test tester = new Test() can be broken down like so:

  1. Test declares a variable of object type Test with name....
  2. tester
  3. = assigns this created reference to what evaluated on the right hand side
  4. new creates a new object on the heap of type...
  5. Test() an empty-constructor initializing the instance created by new

Upvotes: 4

ha9u63a7
ha9u63a7

Reputation: 6824

Java works in the way that

TypeName var_name = new TypeConstructor();

Here, TypeName is your class name. You can however, do it in two parts:

TypeName var_name; // IN the beginning


var_name = new TypeConstructor(); // Or using some other method that returns an object of TypeName

However, you need to make sure that it gets instantiated or assigned correctly. Without TypeName, Java cannot resolve the new TypeConstructor() syntax like the way you've done.

One key thing to remember is that TypeName and TypeConstructor must be of the same type with regards to var_name. You cannot do like the following

String var_name;  // I declared var_name of type String
var_name = new Tester(); // I instantiate var_name to have defaults of type Tester - fails

"Instantiation" and "declaration" are keys here - You can simply leave an "Uninstantiated" variable var_name and Java wouldn't scream. See the example on IDEONE. It's like a job contract that you sign with your employer (t&c applies). Once you sign it (declare Test to be of type Tester), you cannot simply go back and say that "I will be making "Test"s of type Tester" (i.e. Test = new Tester()). You are breaking the t&c of contract, unless you arrange a new contract.

Upvotes: 1

Masudul
Masudul

Reputation: 21971

Java is not dynamic typing language like Ruby or JavaScript. It is static typing language. So, for compiler you need to define type for all variables.

Upvotes: 0

Neeraj Jain
Neeraj Jain

Reputation: 7730

Here is an excellent tutorial about How to Create Objects in Java

Tester is a reference variable of class Test which compiler does not able to know untill you specify in a way which Java Suggest

ClassName referenceVariablName=new ClassName(); //You can also pass arguments

Upvotes: 1

Decagi
Decagi

Reputation: 21

When you create a new object in java you have 4 Keywords which are required. The Type(Class) of the object, the variable name = new constructor call(). Unless you have made a previous declaration such as this : Test Tester; you cannot simply type Tester = new Test().

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234715

You need to tell the compiler what type Tester is.

You might think the compiler could work this out given you're creating a Test(), but it can't. This is because Tester can be many things; typically an interface that Test() implements, or a base class.

E.g. you could also have written java.lang.Object Tester = new Test().

Upvotes: 7

Related Questions