Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Java Programming: Difference between Public void and Public Static void?
Hello i have been taking it slowly learning Java for the past week or so and ive been making my own little programs to help me to learn it as i go along and somthing that has been in the back of my mind is What is the different between Public Void and Public Static void..
so, this is a method Header im pretty sure? and i was uses different classes to call methods and i was use Public void although when i tried useing the Same class as my main method it did not work and i had to use Public Static Void in order for it to work? im quite confused is there any way someone can explain these to me ?
So, what would be the point in using a Non-static method? if it just means u need to write more code?
7 Answers
- JoeLv 41 decade agoFavorite Answer
You can call static methods just by using class name, without actually having an instance of that class.
For example,
MyClass.DoSomething();
With a method that is not static you'd have to do something like this:
MyClass obj = new MyClass();
obj.DoSomething();
BUT static methods do not have access to any instance variables.
- 6 years ago
This Site Might Help You.
RE:
Java Programming: Difference between Public void and Public Static void?
Hello i have been taking it slowly learning Java for the past week or so and ive been making my own little programs to help me to learn it as i go along and somthing that has been in the back of my mind is What is the different between Public Void and Public Static void..
so, this is a method...
Source(s): java programming difference public void public static void: https://shortly.im/MHmpn - Anonymous5 years ago
For the best answers, search on this site https://shorturl.im/avl8J
Not exactly sure what you're asking, but the main difference I see is that the first is an instance method and the second is static. You haven't provided info on the classes that these methods go with, but in the first case you need an actual instance of that class (say a JPanel), and then you call the paint method, passing in a graphics object like myPanel.paint(g). However, in the second one, you don't need an instance of the class since the method is static. So you do this by calling the class name and the method. In this case, it might be used to put something into the graphics object. I don't know what "drawMan" means, but take literally, one might expect this method to take the Graphics object passed in and draw a stick figure man at the point (xpos,ypos). It would not be drawing to a Panel or Canvas because there's no instance to draw on - so I'm guessing the only object to affect is the Graphics one passed in.
- 1 decade ago
If I recall correctly, 'static' refers to the non-instantiated object (a class), thus 'classname'.staticMethod will call it a method from a class file. This is away from the object oriented nature of Java.
However, a method without 'static' is bound in the instantiation of its class (an object). 'objectname'.method will call it.
Don't quote me on it though! Someone else could also be of help I'm sure.
Source(s): first year uni studies. a year ago (!) - Anonymous1 decade ago
You dont have to create an instance of the class before calling a static function, it roughly equates to a global function / variable / class
(nonstatic method)
myClass a = new myClass();
a.DoFunction();
(static method)
myClass.DoFunction();