• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instantiation Question

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And always remember the
First Rule Of Optimization: Don't!
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
reply
    Bookmark Topic Watch Topic
  • New Topic