• Post Reply Bookmark Topic Watch Topic
  • New Topic

static methods  RSS feed

 
Jaime Caetano
Ranch Hand
Posts: 32
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure about the meaning of static method, right now what I think is

if I declare a method as Static, its because it's not gonna be related to an object.

is that right? what else could be behind this subject?




Thank you, Jaime
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaime Caetano wrote:
if I declare a method as Static, its because it's not gonna be related to an object.
Thank you, Jaime


That's correct. If you think of a class as a cookie cutter, static methods are functionality that the cookie cutter has (e.g getCutterSize()), while non-static methods are functionality that the resulting cookies have (e.g. getCookieType()).
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree. You can have cutters of different sizes, so the result will differ from cutter to cutter, so that method cannot be static.
You can have ginger cookies, chocolate, fruit, etc., but that is a function of the cookie, not the cutter, so that doesn's count.

Remember: it is normal for methods to be non‑static and things static are unusual. Never make a method static if any of the following applies:-
  • 1: If there is going to be any difference in behaviour from instance to instance,
  • 2: If it is a method you are overriding, or have overridden,
  • 3: If it is a method from an interface (except possibly in Java8 which introduces static interface methods).


  • You would usually make a method static if it ends with 68 in the most dubious thing on the Ranch: the Campbell Ritchie classification of methods.
    Methods which are called before any instances are available, e.g. the main method, factory methods, are usually static.
     
    Joel Christophel
    Ranch Hand
    Posts: 250
    1
    Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Disagree. You can have cutters of different sizes, so the result will differ from cutter to cutter, so that method cannot be static.


    But the cutter is the class. It is the template for making cookies (objects). Obviously you can have differently-sized cutters, but they would represent different classes. If you had a class LargeCookie, the class can be viewed a cookie cutter that can only produce large cookies.

    Campbell Ritchie wrote:
    You can have ginger cookies, chocolate, fruit, etc., but that is a function of the cookie, not the cutter, so that doesn's count.


    I never claimed that the type of cookie is a function of the cutter:

    Joel Christophel wrote:
    non-static methods are functionality that the resulting cookies have (e.g. getCookieType())
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Joel Christophel wrote: . . . Obviously you can have differently-sized cutters, but they would represent different classes. If you had a class LargeCookie, . . .
    You would never have a LargeCookie class. You would have a Cookie class with size, flavour, etc fields.

    You were ambiguous about types of cookie earlier. Sorry if I misread what you wrote.
     
    Rico Felix
    Ranch Hand
    Posts: 411
    5
    IntelliJ IDE Java Linux
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A static method is one that belongs to the class itself and it not tied to any special instance of the class. To clarify this statement, let's break it down by using some visual and further explanation.

    Static methods are mainly used to implement utility functionality. To keep it simple lets demonstrate a trivial example.

    Lets say you want to get the area of a square. In order to calculate the area you must know the length of one side, therefore you can implement a method to do the work for you by giving it the length of one side and it returns the area.



    By looking at this simple code fragment you can realize that the method is not dependent on the state of a particular instance of some class, therefore you can make it static which says that the method can be called without created an instance of the class that this method resides in. It belongs to the class and can be called by just using the class name.

    Example:



    Now having the method declared as static, to find the area of a square you would not have to say new SquareAreaCalulator().area(12.5) which is creating an instance of the class to call the area method.
    No, instead we call the area method on the class itself by saying SquareAreaCalulator .area(12.5) which can be called many times passing in any arbitrary value without a single instance of SquareAreaCalulator being creating to use that method and the functionality of the method will behave exactly the same each time.

    On the other hand, when functionality is dependent on the state of a particular object of a class we don't use static.

    Lets say you have a Circle class that you can create circles from which each circle has a different radius. When you call area on an instance it will give you the area for that particular instance and the value returned will be dependent on the specific radius value of the circle. The area method in the circle class functionality is dependent on the state of a specific circle object.

    So we have the following code:



    To use this class we have the following:



    From this we can see that in order to get the area for a circle we have to create an instance of Circle and pass in a radius value at creation time. This value will be used to get the area for a specific circle.

    All that has been explained here is dependent on design requirements.
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nice example
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jaime Caetano wrote:if I declare a method as Static, its because it's not gonna be related to an object.

    Correct.

    I don't want to repeat all the good advice you've been given, but I will say this:
    Java unfortunately allows you to also call static methods on instances, so in Rico's example above you could write:My advice: DON'T.

    There are certain, VERY obscure, cases where this feature has uses; but in general you should ALWAYS call static methods by their class name, viz:
    SquareAreaCalculator.area(2.0)

    Indeed, I use that format even when I'm calling a static method in the same class, just to remind myself that it is actually static.

    HIH

    Winston
     
    Jaime Caetano
    Ranch Hand
    Posts: 32
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks for helping me, I really appreciate it ;D
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!