This question already has answers here:
What is the difference between a static method and a non-static method?
(13 answers)
Closed 4 years ago.
So, I'm pretty new to Java, and as far as I can see, both of these are the same things:
public class HelloWorld {
public void test(String test) {
System.out.println(test);
}
public static void main(String args[]) {
HelloWorld helloworld = new HelloWorld();
helloworld.test("Hello world!");
}
}
and
public class HelloWorld {
public static void test(String test) {
System.out.println(test);
}
public static void main(String args[]) {
test("Hello world!");
}
}
Are these both the same thing, and what's the reason you would use one over the other?
Static methods sometimes are difficult to test.
Non-static methods specify the behavior of objects. Static methods are typically for utility functions (such as Collections.sort())
Related
This question already has an answer here:
Calling static generic methods
(1 answer)
Closed 4 years ago.
For example In my main class I have
public class main
{
// main method
public static <T extends Building<T>> T houseAll(T input)
{
// random information
}
public static void main(String[] args)
{
// This is where I will make the call
}
}
So how would I make the call in the main method from a generic static method thats in the main class?
What wrong using:
public class main {
// main method
public static <T extends Building<T>> T houseAll(T input) {
// random information
return null;
}
public static void main(String[] args) {
// This is where I will make the call
houseAll(null);
}
}
This question already has answers here:
What is the difference between static and default methods in a Java interface?
(12 answers)
Closed 5 years ago.
interface TestInterface
{
public static void square(int a)
{
System.out.println("Square is "+a*a);
}
public static void show()
{
System.out.println("Default Method Executed");
}
}
class TestClass implements TestInterface
{
public void square(int a)
{
System.out.println(a*a);
}
public void show()
{
System.out.println("Overridden Method");
}
public static void main(String args[])
{
TestClass d=new TestClass();
d.square(4);
TestInterface.square(4);
TestInterface.show();
d.show();
}
}
I have a doubt in my code. I learnt that static methods cannot be overridden in JAVA, but it seems to be working fine here.
When i give both default and static keywords together, like this
interface TestInterface
{
default static void square(int a)
{
System.out.println("Square is "+a*a);
}
public static void show()
{
System.out.println("Default Method Executed");
}
}
An error crops up as follows:
illegal combination of modifiers: static and default
What is the reason for JAVA treating this as an error?
A static method is meant to be called without an instance of the class/interface concerned. Usually they are meant to be utility methods.
A default method is meant to be called on an instance of the interface concerned. All implementations of this interface will have this method definition, unless it is overridden.
The reason these two terms are not allowed together is simply because they contradict each other: default requires an object, static requires no object.
TestClass.show() and TestClass.square() are not static and therefore do not override the static methods in the interface. They are member methods and require an object to call them. On the other hand, the methods with the same name in the interface are static and so you can call them with the interface name or class name without an object.
This question already has answers here:
Why doesn't Java allow overriding of static methods?
(22 answers)
Closed 6 years ago.
Is this method type hiding? How static method gets area in memory?
public class Demo {
public static final void main(String args[]) {
try{
A a = new B();
a.display();
}catch(Exception e){
e.printStackTrace();
}
}
}
class A{
static void display(){
System.out.println("A");
}
}
class B extends A{
static void display(){
System.out.println("B");
}
}
Static methods dont belong to a instance but to a class, overriding some static method makes no sense and java will not let you to do that
This question already has answers here:
Cannot make a static reference to the non-static method
(8 answers)
Closed 9 years ago.
I have single class in java having two methods. one is public static void main(String args []).
when i call other method inside main i get above error.
class Test {
public static void main(String[] args) {
method();
}
private void method() {
System.out.println("hello");
}
}
When this is unclear, you should follow some basic java tutorials first.
This is a nice start: What is the best java tutorial site.
Solution - make the other method static as well - OR make an instance to it through the class Test (using the new operator)
ALTERNATIVE 1 (using static)
class Test {
public static void main(String[] args) {
method();
}
private static void method() {
System.out.println("hello");
}
}
ALTERNATIVE 2 (using an instance of the class Test)
class Test {
public static void main(String[] args) {
Test test = new Test();
test.method();
}
private void method() {
System.out.println("hello");
}
}
Creating an instance of Test, because non static methods should be called from instance:
new Test().method();
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the reason behind “non-static method cannot be referenced from a static context”?
public void Sort(){
*some code*
}
public void displayResults()
{*more code*
}
public static void main(String[] args)
{
Sort();
displayResults();
}
Why am I getting this error? I have sort(); in another abstract class and then this class here is extending it.
-Confused
You need to instantiate the class that contains Sort(), displayResults() and main before you can call Sort() or displayResults() from main().
class Example {
public void Sort(){
// *some code*
}
public void displayResults()
{
// *more code*
}
public static void main(String[] args)
{
Example ex = new Example()
ex.Sort();
ex.displayResults();
}
}
You need an instance of a class to call a non-static method. Calling from a static method, you don't have an instance, as statics are associated to a class, not to an instance. Therefore, you are not allowed to call non-static methods or access non-static variables from inside a static context.