0

I have below classes in the same package:

class Roo {
    public String doRooThings()
    {
        return "Rooing!";
    }
}

class Cloo extends Roo {
    public static void main(String[] args) 
    {
        System.out.println(doRooThings());
    }
}

I get Cannot make a static reference to the non-static method doRooThings() from the type Roo error upon executing this.

But I can call the doRooThings() method (without having to preface it with a reference) from a public method in child class like below :

class Cloo extends Roo {
    public void testRoo()
    {
        System.out.println(doRooThings());
    }
    public static void main(String[] args) 
    {
        new Cloo().testRoo();
    }
}

Can anyone please let me know how this works?

Touhid K.
  • 351
  • 1
  • 5
  • 23
  • You do realise that you are creating a reference in the second case. "new Cloo()". Your statement itself "without having to preface it with a reference" is incorrect here. Perhaps you meant something else? – maheeka Dec 03 '14 at 09:29
  • Your main() is static, meaning it doesn't have access to any instance variables. Maybe reading this would help to explain what static means: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – kha Dec 03 '14 at 09:30

4 Answers4

2

Because the main() method runs in a static context. Static means you have no valid instance of the class. That means Java doesn't know which instance to call that method on...

Read some basics like this explanation.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
2

main is a static method, therefor it is not associated with any instance of the class. As a result it can not call an instance method(as it needs to be associated with an instance) unless it first creates an instance. Maybe you need to read more about the meaning of static?

Community
  • 1
  • 1
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • I am creating an instance of Cloo in main method. So, Does that mean this instance will be shared throughout the class? – Touhid K. Dec 03 '14 at 10:22
1

Because your method doRooThings is not static while your main method is a static. you can call doRooThings by 2 options:

First

create an object of Roo on your main method Roo roo = new Roo(); then call it by roo.doRooThings();

Second

change your method doRooThings() to static, public static void doRooThings();

Additional

if we create a static method or an static instance variable it means that it is owned by that class.

on your new Cloo().testRoo(); you are able to call the testRoo() method because you created a new instance of Cloo class by having a new keyword. it is the same of my first suggestion the only difference was on my first suggestion I create a new instance of Roo class and reference it to roo

Community
  • 1
  • 1
Secondo
  • 451
  • 4
  • 9
0

In first case main method is static and method doRooThings() this non static method. Inside static method you can't refer a non-static (instance) method.

But in second case you are able to call non static method testRoo() because your creating an instance within the static context. Similarly even in first case you call the non static method by creating instance as like below:

new Roo().doRooThings() or new Cloo().doRooThings()

Suresh
  • 16
  • 1