Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to call non static method?

843789Jan 11 2010 — edited Jan 12 2010
I have written code as follows
abstract class Test
{
void a()
{
System.out.println("in abstract class");
}

}

class Test1 extends Test
{
void a()
{
System.out.println("in main class");
}

public static void main(String args[])
{
System.out.println("in main method");
a();

}

}
This is showing compile time error that non-static method a() cannot be referenced from a ststic context.

After that i wrote this
abstract class Test
{
static void a()
{
System.out.println("in abstract class");
}

}

class Test1 extends Test
{
static void a()
{
System.out.println("in main class");
}

public static void main(String args[])
{
System.out.println("in main method");
a();

}

}
then this is running fine.But i dont want to make that method static.Is there any other solution for that?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 9 2010
Added on Jan 11 2010
20 comments
1,533 views