• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating object of class  RSS feed

 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible for an instance of class to be created within itself? For example, if ClassA is running and a certain button is pressed (defined in classA) then a new instance/object of classA is created - so that for this case the original object is running as well as the new one. (2 programs of the same class running?)
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course. Objects of any type, including inside its enclosing class, can be created anywhere. This is how the Singleton pattern works, the Singleton class has a private constructor and maintains a single instance of itself and has a static "getInstance" or other appropriately named method to retrieve the instance.
 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So are you saying that creating an instance of the class - like you normally would will work? ie,
public class classA{
public void Button(){
......
if (Button pressed){
classA a = new classA();
}
else......

}
}
I know that this would not work in a program - I am just doing it for illustrative purposes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!