• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declare and Instantiate an Object in Two Steps

 
mahesh rao
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java code must be either be in an initializer, in a constructor, in a method, or part of a variable declaration.

Henry
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic