• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating an object from a static method

 
nuala finnegan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new to Java and this forum so apologies if this is a very basic/stupid question!

I have a static method which reads in an external file. The external file contains 2 string which go on to populate instance variables...
As in, I create an instance of the class from a static class.

The instance I create is called DS1. How do I use DS1 in a separate test file to access other non-static methods..

I hope that makes sense! In other words if a static method can create an instance of a class how can that instance be accessed?


Any advise would be much appreciated!

Thanks,

nuala
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can the static method return a reference to the object?
 
Junilu Lacar
Bartender
Pie
Posts: 7747
60
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nuala,

Welcome to the Ranch!

The things that matter here:
1. That your static method returns a reference to the object that it created
2. That you have code that is assigning the reference to a variable.

If you have this, then you can use the object from outside of the static method that created it.
 
nuala finnegan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Both

Thanks very much for your replies.... it works now. much appreciated!

Nuala
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
What you are describing is probably called a factory method. There is a Wikipedia page about that, but they chose to instantiate complex numbers, which are not easy to read. There is an easier example to understand in Joshua Bloch’s book Effective Java™, pages 5-16. I recommend you read that, if you can find a copy.
We do not usually hand out code, but this is the sort of thing which would be unfamiliar to you, so it is appropriate here. The following class is written in such a way as to be immutable, and has a private constructor, so the only way you can get instances is via the factory method. Because of this, there are all sorts of other fiddles you can work, including caching instances.Bloch says that names for factory methods include getInstance, newInstance, valueOf, of, getType and newType. In most cases, they must be invoked before an instance of the class is created, so they are marked static.
 
joseph dela cruz
Ranch Hand
Posts: 38
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does it mean we can construct multiple objects with specific attributes(constructor) in one class? sorry if this is another stupid question but i want to know do i really have to make that much classes just to make multiple objects?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you mean, “can we use such a method several times to create several objects with different attributes?”, then, yes. And yes, you can use such methods to call different constructors, but it is probably easier to use overloaded methods to call overloaded constructors.
You don’t have to use factory methods; this can all be done with direct constructor calls.

Find the Effective Java™ section I mentioned earlier. It explains this question far better than I can.
 
Junilu Lacar
Bartender
Pie
Posts: 7747
60
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joseph,

I'm not sure you are completely clear on some of the terms/concepts so I suggest that you read up on those first: http://docs.oracle.com/javase/tutorial/java/concepts/

A class can have multiple constructors. It can also have no explicit constructors, in which case the Java compiler will provide a default no-argument constructor. It is up to the programmer/designer to decide how many constructors are appropriate for a class.

You normally create objects by using the new keyword followed by an appropriate constructor. For example, new Student() will create (allocate memory) and initialize a new instance (object) of the Student class.

You don''t have to keep apologizing about your questions. We try to be very understanding here and give newbies a pretty hefty allowance of "stupid" questions. BTW, there is no such thing as a "stupid" question, as long as you ShowSomeEffort (<-- click on link).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic