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?
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?
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");
}
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.