File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 18
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: 16809
    
  19

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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Declare and Instantiate an Object in Two Steps
 
Similar Threads
Looking for explanation
jq+ Question
instanceof operator with null
instanceof
Java Rule Round-Up