aspose file tools*
The moose likes Beginning Java and the fly likes Static and non Static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static and non Static methods" Watch "Static and non Static methods" New topic
Author

Static and non Static methods

feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
How can I call a non static method from static method knowing that both belongs to the same class ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
You need to create an instance of that class; then you can use that to call non-static methods.
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by feda alshahwan:
How can I call a non static method from static method knowing that both belongs to the same class ?




You can create an object and call that !

sample code





:roll:

Remember :


# static methods / static fields are always class members.

# non static methods and fields are always instance members.




Discussion - the powerfull way to excellence!
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
Thank you very much your example code was very clear.
Marcus Rag
Greenhorn

Joined: Dec 23, 2009
Posts: 6
Hi there,

I have a similar situation. However, I may call several instances of this object while my program is running. This will obviously lead to a waste of resources.

Once I have finished using ONE instance of this object, how do I completely clear it from the PC, so as to avoid wasting resources? Can I simply use "object.dispose();"?

The reason my object is so resource hungry, is because it is and entire JFrame class that needs to be instatiated everytime I need it.

Thank you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Welcome to JavaRanch

You don't do anything to get rid of an object; once it is not referred to it will automatically be removed from memory. Why do you have to instantiate the JFrame several times?
Marcus Rag
Greenhorn

Joined: Dec 23, 2009
Posts: 6
Thank you for the kind welcome...SO happy to have found this place!!! :)

It's hard to explain over a message, so please bare with me...

I have two JFrame forms (winStart and winLogCall). winStart (which is the main window and is ALWAYS open, even if other JFrame forms are open), has a dialog which I need to access from winLogCall (which is only opened as the user needs it), but because calling this dialog is a static method, it causes an error. The way around this error is instantiating a winStart object, calling the dialog, using it, and then closing the winStart object I instatiated to save resources.

I suppose another question would be, how do I call components of an instatiated object (winStarts' dialog for example), from another instatiated object (winLogCall) without instatiating winStart everytime (remember that winStart is open all the time anyways).

I have tried making the dialog an independant JFrame form itself, but that did not eliminate the static to non-static method errors and again I would need to instatiate a whole bunch of those objects...

I don't understand that an instatiated object does not take memory...If it is instatiated, it exist and is stored in the memory, so that the program knows what's happening. Campbell, I am not contesting you here, I am just voicing how understand what is going on. I have very limited knowledge (I finished school 3 weeks ago) so most of what I am writing here, is new to me. We did a lot of concepts etc. at school, but the is still weird for me. Please explain, if you can, how JRE handles instatiated objects to avoid memory wasting, without having the running program "forget" what object exist.

cheers
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I never said an object doesn't take memory. What I meant was the garbage collection is automatic, so the object is removed when no longer required.

Afraid I don't yet understand the design of your application. Are you separating the display and the model (=the data) and the control from one another? Don't understand how a static method causes an error. Sorry.
Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
I don't understand the point here:

Why would anyone want to call a non-static (instance) method from a static method? What is the design requirement to do so? (Only exception is the main static method from which you want to call non-static methods).

static methods have class wide scope and they provide utility value to calsses i.e. for example keep count of the number of objects that are alive, the utility functions in the Math class etc.

Instance (non-static) methods are used to change the state of an object i.e. the values of its instance variables.

The only place I see where a static method would need to call a non-static method is the main method of a class where you want the code to execute. So you have to make objects inside the main method (which is a static method) and then call the non-static methods on those objects like shown below:

public class Dog
{
private String name;
private static int numDogs;

public Dog(String name)
{
numDogs++;
this.name = name;
}

public String getName()
{
return name;
}

public static void main(String[] args)
{
Dog d1 = new Dog("Jimmy");
Dog d2 = new Dog("Teri");
//accessing non-static method (getName) from a static (main) method
System.out.println("we have " + numDogs + " dogs " + d1.getName() + " and " + d2.getName());
}
}

Welcome any comments.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2864
    
  11

[quote=James Tally]
It's hard to explain over a message, so please bare with me...
[/quote]

I'm not sure how nudism helps, but since you said please ...

Boy, my co-workers are really giving me some nasty looks!


I agree with other posters that you need to examine what you are doing. Why do you need to call a non-static method from a static method? Does the non-static method rely on non-static fields of the class? No? Then make it static. Yes? Then, maybe those fields could be static as well.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
"Nudism"?? There ought to be laws against that sort of joke.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static and non Static methods