Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compilation error

Status
Not open for further replies.

sujitopics

Programmer
Oct 9, 2001
69
0
0
KR
Dear friends,
please help me.

I have to close B_Frame (Child frame) from A_Frame (Parent) with a button click.

In A_Frame.java (Parent Frame) I used like this :

public void closeChildFrame()
{
System.out.println("close()");
B_Frame.hide();
B_Frame.dispose();
}

Compilation error :

non-static method hide() cannot be referenced from a static context B_Frame.hide();
^
non-static method dispose() cannot be referenced from a static context
B_Frame.dispose();


please help me.
your kind cooperation would be greatly appreciated.
Thanks in advance.
 
hi!

the compiler complains that you r calling a non static methods from a static context(function or object....)

i dont see it in the code you supplied but if you show the rest of your code it might be usefull.

raikyng.
 
You cannot call non-static (i.e. object methods) from static (i.e. Class) methods.

Static methods may only call other static methods and may only use static member variables.

Non static methods have access to all.

e.g.

public class Test
{
int a = 10;
static int b = 20;

public void foo()
{
a = b; // OK, can use static variable
bar(); // OK, can use static method
}

public static void bar()
{
b = a; // WRONG, cannot use non static variable here
fool(); // WRONG, cannot call non static method
}
}

The reason is that static methods and variables are available once the ClassLoader has loaded the class. Non-static methods and variables require an instance of a class (i.e. an object) in order to be used.
 
Hi sujitopics,
And so if you create an instanc of B_Frame as
B_Frame bfr = new B_Frame();
and then call the methods as
bfr.hide();
bfr.dispose();
then your program would compile without complaining.

Hope this helps.
Regards,
Rajarajan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top