Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating objects in the same class

 
Mukul Anand
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does it mean to create an object of a class in that same class??
I mean whats the logic??
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In what context are you talking? Where did you found this??
This may be in context of some design pattern, some strategy to take control over constructors or factory methods. Please be specific.
 
Mukul Anand
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well.. sometimes, an object of a class is created within the same class.. Usually, we create an object for a class in another class. Isn't it??
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on your requirement. Lets say i have class A


In both the cases object will be same, only visibility might be different, but that again is valid for access modifiers. The contents of object(i.e. members and variables) will be same.
I mean to say that, object of a class will be same, wherever you create it.
 
Campbell Ritchie
Marshal
Posts: 53094
124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you thinking about singletons? Or factory methods?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15748
74
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mukul Anand wrote:What does it mean to create an object of a class in that same class??

It doesn't mean anything special; it doesn't matter if you create an instance of a class inside a method of that class, or inside a method of some other class.

Is there a specific scenario that confuses you? If so, then please provide a code example and we can explain what's happening.
 
Mukul Anand
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing specific.. but, I used to believe that an object of a class is and should be made in some other class. So, when I noticed this for the first time, I got confused.
 
Campbell Ritchie
Marshal
Posts: 53094
124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There can be good reasons why a class creates an instance of itself inside itself. I can think of three reasons offhand (two already mentioned)
  • 1: Singletons
  • 2: Factory methods.
  • 3: Self-referential classes (eg for linked lists).
  •  
    Mukul Anand
    Greenhorn
    Posts: 29
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Okay.. thanks all for your replies
     
    Campbell Ritchie
    Marshal
    Posts: 53094
    124
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You’re welcome
     
    What are you doing? You are supposed to be reading this tiny ad!
    the new thread boost feature brings a LOT of attention to your favorite threads
    https://coderanch.com/t/674455/Thread-Boost-feature
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!