Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

When to use static, static void?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 26963
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74753
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic