This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 18546
    
  40

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
Java Rule Round-Up
instanceof operator with null
instanceof
jq+ Question
Looking for explanation