aspose file tools*
The moose likes Beginning Java and the fly likes Declare and Instantiate an Object in Two Steps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declare and Instantiate an Object in Two Steps" Watch "Declare and Instantiate an Object in Two Steps" New topic
Author

Declare and Instantiate an Object in Two Steps

mahesh rao
Greenhorn

Joined: Sep 08, 2004
Posts: 19
I am not sure why the following does not work.

public class Test {
.....
}

class Test2 {
Test t;
t = new Test();
}

But this one works.

public class Test {
....
}

class Test2 {
Test t;

public void setup() {
t = new Test();
}
}

Can anyone explain it. Thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39

Java code must be either be in an initializer, in a constructor, in a method, or part of a variable declaration.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declare and Instantiate an Object in Two Steps
 
Similar Threads
instanceof
jq+ Question
Looking for explanation
Java Rule Round-Up
instanceof operator with null