Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Creating an object from a static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating an object from a static method" Watch "Creating an object from a static method" New topic
Author

Creating an object from a static method

nuala finnegan
Greenhorn

Joined: Jun 15, 2012
Posts: 5
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

Joined: Oct 02, 2003
Posts: 11256
    
  16

can the static method return a reference to the object?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
nuala finnegan
Greenhorn

Joined: Jun 15, 2012
Posts: 5
Hi Both

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

Nuala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
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

Joined: May 17, 2012
Posts: 38

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

Joined: Oct 13, 2005
Posts: 38472
    
  23
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

Joined: Feb 26, 2001
Posts: 4456
    
    6

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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating an object from a static method