wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Instantiation Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instantiation Question" Watch "Instantiation Question" New topic
Author

Instantiation Question

Tom Keith
Greenhorn

Joined: Mar 26, 2002
Posts: 24
Hi All,
Can somebody clarify the following for me please.
Consider the following two classes:

I have a thrid class as follows:

What are the pros and cons of using Access Method1 & Access Method2. Does the second one improve any performance?
Thanks,
Tom
[ June 10, 2002: Message edited by: Dirk Schreckmann ]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Tom
Accessing what? The String in class1?
The two samples for Class1 are pretty much the same. As far as the default constructor, the compiler will create one for you anyway so it doesn't matter if you include it or not. For the getName method in the first class it is an instance method meaning that it will work on an instance of a Class1 object. You would use this if each instance of the class had it own name so that the name returned would be unique to that instance.
You would use the static method if the String that it is returning is a static variable - there is only one for all instances of the class and if one changes it is changed for all of the other instances that might be created.
In your Class2 they are the exact same thing except that in the first one you are creating an instance of Class1 and then using it to call the getName method, in this case it'd work if getName was static or if it was an instance method. Although, if it is static, it is more common to just use the class name to call the method:
Class1.getName();
That makes it more clear that you are calling a static method.
In the second example of Class2 you don't create an instance of Class1 and assign it to the c1 variable so, unless it is done elsewhere in the code, you'd get a compiler error when you try to use the variable. But the method call is the same and would depend on if the method were static or not.
At this point it is more of a design issue as opposed to a performance issue, meaning - does the method need to be static or not. If it is not static then you have to have an instance to call the method on. If it isn't static then you can call the method on the just the class name. If that is the case then you don't actually need to create an instance of Class1 and that would give you better performance.
Hope that answered your question.
[ June 10, 2002: Message edited by: Dave Vick ]

Dave
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

So basically you're asking why you would want to make the access methods in a class static or non-static??
I don't think its really pros and cons for the two -- but more a matter of design -- do you want your class to be able to be instatiated? so each INSTANCE has these variables? or do you only want one?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
And always remember the
First Rule Of Optimization: Don't!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Keith Smith
Greenhorn

Joined: Apr 18, 2002
Posts: 6
Once you declare a method to be static all other methods of that class that you start accessing from the first static method need to be static.. SO you're heading towards a bad design.
I would say unless it is your top level class avoid it.
Tom Keith
Greenhorn

Joined: Mar 26, 2002
Posts: 24
Why is a bad design to use static for methods & variables. Couple of my friends said that was the best way than to instantiate the class and call its method. I thought declaring Methods and Variables static would put them in memory and pick them up from there whenever called as compared to instantiating the class where eachtime a new instance is created unless & otherwise it is created as an Singleton. Can somebody clarify for me please.
Thanks,
Tom
Tony Jackson
Ranch Hand

Joined: Feb 23, 2001
Posts: 45
Tom and Keith,
I don't think it is bad design at all to use static fields and methods. It depends on what the method does and what the class does.
If the method requires an instance of the class to do its thing, then it makes sense to make it an instance method. But if a member DOESN'T require an instance of the class to do its thing, then why bother making it an instance method? You are creating make-work for the person who is going to use your method. They have to first create an instance of your class (even though they don't really need it for anything else) and then they can call your method.
An Example: If you want to call the sqrt (square root) method from the java.lang.Math class, do you want to have to create a Math object first? No! You need to have a numeric primative (int, float, etc.) and you want to call the method and get back a numeric result. So it makes sence that the sqrt method is static.
But, as Keith asked, what if the sqrt method wanted to call a non-static method? My response to that is: why would it ever want to do that? If sqrt doesn't require an object, then it shouldn't ever need to call a method that DOES require an object.
Tony Jackson
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Tony Jackson:
An Example: If you want to call the sqrt (square root) method from the java.lang.Math class, do you want to have to create a Math object first? No! You need to have a numeric primative (int, float, etc.) and you want to call the method and get back a numeric result. So it makes sence that the sqrt method is static.
But, as Keith asked, what if the sqrt method wanted to call a non-static method? My response to that is: why would it ever want to do that? If sqrt doesn't require an object, then it shouldn't ever need to call a method that DOES require an object.

Of course, this example only exists because we have primitive values in Java. If numbers where objects, the sqrt method should be an instance method of these.
But there are other examples for the use of static methods. A factory method is one of them: http://www.refactoring.com/catalog/replaceConstructorWithFactoryMethod.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instantiation Question
 
Similar Threads
Thread synchronization
Accessing component from main class.... Help!
Static example
How to make a Variable public to all classes ?
Protected members