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 OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP 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: 19235
    
  41

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