• Post Reply Bookmark Topic Watch Topic
  • New Topic

When should you/can you use static variables and methods  RSS feed

 
Ranch Hand
Posts: 332
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know what static variables and methods are

when the static keyword is used it makes the method or variable available to the program without having to instantiate an object from the class
i get this.

but when is the right time to apply this...heres my example and you can tell me if its ok or should i be doing something else

i have a class called VariousFunctions

and in this class I have a method called
mainMenu

it basically prints out the selections available but also handles user input and logic for the menu system

is this perfectly ok to make a static method?

or are there caveats or pitfalls when programming like this...
 
jon ninpoja
Ranch Hand
Posts: 332
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just found this after posting

https://coderanch.com/t/651189/code-menus-menus

and hes using static so i guess its ok

 
jon ninpoja
Ranch Hand
Posts: 332
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after reading further i see its not ok
 
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short answer: never.
Slightly longer answer: only when you are confident you are working outwith the scope of any object.
Longer answer still: only when you are confident you are working outwith the scope of any object. That includes code which has to be run before any objects are available.

Constants don't vary, so nothing happening in an object affects them, so constants are usually declared static.
Methods which are pure functions come out as 1368 in the most dubious classification of methods known to modern science; they are therefore usually made static. You can see examples of pure functions in utility classes: java.lang.Math is a good example. Note it also has constants in.
Factory methods usually have to be called before any instances are available, so they usually have to be static. You will notice many factory methods are technically pure functions.
 
jon ninpoja
Ranch Hand
Posts: 332
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell

Enums are to do with constants too arent they?,so when would you use a static method and when would you use an enum? or am i missing something here?
 
Sheriff
Posts: 23486
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jon ninpoja wrote:so when would you use a static method and when would you use an enum? or am i missing something here?



I think you're missing something. Those are two separate questions: "When would you use a static method" and "When would you use an enum". There's no particular connection between the two.
 
jon ninpoja
Ranch Hand
Posts: 332
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do they not both deal with constants?
 
Marshal
Posts: 5621
387
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enum isn't a method as opposed to static method. Enum is in some sense a special data type, which appear to be constant (in most cases a set of constants). While static method is method which appear to be static.
 
Paul Clapham
Sheriff
Posts: 23486
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jon ninpoja wrote:do they not both deal with constants?



I don't think so. Could you elaborate on why you think a static method is, or deals with, a constant?
 
Rancher
Posts: 785
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon,

There are 4 instances that I can think of where I use static:

1 -- Factories: I don't want to have to instantiate a factory just to use it.
2 -- Class Variables: they are like wormholes, they are the same throughout the universe. If you don't know what that means, then don't use them.
3 -- Interfaces: you can add static and default methods to an interface and not have to recompile the project--static and default are not the same thing.
4 -- constants: static final--they are what they are and you cannot change them anywhere.

There are probably more, but the very best thing I can say is: as a general rule of thumb, don't use them. So unless you know you really want them, what they do for the instance you want, and specifically what the side effects are in using them.
 
Saloon Keeper
Posts: 8720
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The most important three for me are:

  • Constants,
  • private pure functions, and
  • static factory methods for value types.

  • The declaration of constants should be self evident. Most of the time, you would use an enum for these, but sometimes you want to declare constants of some primitive or other immutable type.

    By private pure functions, I mean static methods that have parameters, and return something based on the values of those parameters, and nothing else. The reason to make them private is that they should only be called from other methods in the same class, to break up those methods into smaller chunks and make them self-documenting. If you have to write a pure function that's not private, it's either because you don't control the types involved in the parameter or return types, your application design is not object oriented, or you're writing a factory method (next point).

    Factory methods are a special type of pure function that can be seen as named constructors. They generally should be named something like createTypeName(Foo foo, Bar bar), parse(String string) or valueOf(Foo foo). In general, with the exception of parsing methods, they should not do much more than call a constructor or retrieve a cached value.
     
    Campbell Ritchie
    Marshal
    Posts: 58823
    179
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Les Morgan wrote:. . . unless you know . . . what they do for the instance you want . . .

    I presume you are using “instance” in the sense of “under these circumstances” rather than “instance of a class”. As I said earlier, pure functions are usually implemented as static methods.
     
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Static variable: When you need something that will be used through out the application and every instance need to know the variable.

    Instance variable: It will be different from object to object and object's property while static variable is Class's property.

    Static function: Used to do some utility task. Can be called without any object declaration.

    Instance function: Need object to call this function.

    static or instance depends on your uses .
     
    Campbell Ritchie
    Marshal
    Posts: 58823
    179
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Jack Ramon wrote:Static variable: When you need something that will be used through out the application and every instance need to know the variable. . . .

    Many people believe there should be no such thing as a static variable. Only constants.

    Static function: Used to do some utility task. . . .

    That is very vague. But pure functions usually are marked static.

    static or instance depends on your uses .

    In an object language like Java®, everything should default to being instance type. Only make things static as an exception to that rule with a good explanation.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!