0

I'm very new to Java, and I just have a few questions that I can't seem to clear up even with some research.

What does it mean to override a method?

Also what is the difference between a static and non-static method?

3
Contributors
2
Replies
6
Views
7 Years
Discussion Span
Last Post by stultuske
0

Also what is the difference between a static and non-static method?

as to this question: a non-static method can only be called through an instance of a class, for instance:

public class StaticNonStatic{
// first a non-static method
public void runNonStatic(String output){
System.out.println(output);
}
// second, a static method
public static void runStatic(String output){
System.out.println(output);
}
}

The next class here, is a short main method that shows how to use these methods:

public class TestClass{
public static void main(String[] args){
// to use a static method, you access that method 
// through the class:

StaticNonStatic.runStatic("Print this through a static method");

// now, to run a non-static method, you'll need an instance of 
// that class, so, first you'll have to instantiate an Object of the type
// StaticNonStatic

StaticNonStatic statNStat = new StaticNonStatic(); 
// using the default constructor
// now you can call the non-static method through this instance
statNStat.runNonStatic("Print this through a non static method");
}
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.