Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When to use static, static void?  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Folks,
I am just a beginner in Java learning Kindly bear with my beginner questions.
I'm getting confused when to use ("Static" & "Static Void")
please explain.


Thank you,
Kim
 
Sheriff
Posts: 23451
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kim, welcome to the Ranch!

Could you give an example in which you don't know which of the two to use? I ask because I don't think there are any situations where you could validly use both of them -- but I could be wrong.
 
Author and ninkuma
Marshal
Posts: 66541
160
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that Java is case-senstive, so static and void. Be sure not to capitalize them either in code, or when referring to them in text.

static and void are distinct concepts. You use static when you want a member to be static (and everything that entails). void is used to indicate that a method returns no value. If the method returns a value, it will indicate the type of that value; such as int or String.

So, for a static method, it's not a choice between static void and static, but between static void and static int, or static String, or whatever other type a method might return.
 
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static is used when you want to reference a method or variable without instantiating an object. An example would be the "main" method which is always declared static. Another example is with the Math object, which has PI declared as static so you can reference it without instantiating an object of Math.

void is the return type of a method that doesn't return anything.

If you have a method that returns an int for example you would declare it



but if your method doesn't return anything

 
Ranch Hand
Posts: 355
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to illustrate what Bear and Brian have said using the following example.


You can realize that we didn't need to create a ClassOne object on which to call the methods aNumber() and voidMethod(). However, we needed to create the object classone and use it to call the method anotherNumber();
 
Marshal
Posts: 58421
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about void method for the time being. There are a few things about static methods.
1: Most important: the rule is that all class members are instance members.
So static members are exceptions to that rule because things static do not belong to an object. They belong to the class. So things static should be unusual. And you only make things static with good reason.
Find the most dubious classification of methods known to modern science; if a method's type ends 68 (usually 1368) it is a candidate for being made static.
Never make a method static if you want to override it or if it comes from an interface. Interface methods are never static (well, not until Java8).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!