Two Laptop Bag*
The moose likes Beginning Java and the fly likes Static question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static question" Watch "Static question" New topic
Author

Static question

Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 63
Java keeps telling me that the bolded method call is static and can't call a non-static method, but I'm not understanding why the call is static. Can someone please clarify for me?



This code is all in HTMLFrame which extends JPanel and is shown in a JScrollPane that is in a JTabbedPane which is on a JFrame.

P.S. setTabTitle(String, Component) is a method I made that makes it easier to set the title of a tab on my JTabbedPane


Life is 10% what happens and 90% how you react to it.
Attitude is everything.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2172
    
  47
Originally posted by Derek Boring:
Java keeps telling me that the bolded method call is static and can't call a non-static method, but I'm not understanding why the call is static. Can someone please clarify for me?


Presumably your declaration of the setTabTitle(..) method uses the keyword static which makes it a static method and hence you are able to call it via the classname rather than having to call it via an instance of the class.

Can you post this method for us to see.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You can call a static method using an object reference. The problem is in trying to call a non-static (instance) method from a static context.

As Tony suggested, it looks like your method setTabTitle is probably static, and somewhere within the body of that method (that is, from within a static context), it is trying to call a non-static method.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 63
Here is the method being called. It's not static.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41106
    
  45
What's the exact error message?

The compiler may think that the method is static because you are trying to call it through the class name (BrowserFrame) instead of through an object. (I'm assuming that BrowserFrame is a class name, and not a field name).


Ping & DNS - my free Android networking tools app
Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 63
Here is the exact error message:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static method setTabTitle(String, JPanel) from the type BrowserFrame

at HTMLFrame.go(HTMLFrame.java:23)
at BrowserFrame.newTab(BrowserFrame.java:36)
at BrowserFrame.<init>(BrowserFrame.java:16)
at startBrowser.main(startBrowser.java:4)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Derek Boring:
...Cannot make a static reference to the non-static method setTabTitle(String, JPanel) from the type BrowserFrame...

As Ulf suspected, BrowserFrame is a type -- not an instance. And you cannot call a non-static (instance) method from a static (class) context.

It looks like you should be using an instance of BrowserFrame to call the method, because the method uses "tabs," which I assume is an instance variable. (If no instance members were used inside the method, then making the method static might be an option.)
Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 63
Ok, I think I understand now. Because BrowserFrame is a class name and not an instance name, it requires a static call to it, right?

So now I have another question:
The code at the very top is in HTMLFrame which extends JPanel. HTMLFrame is in a JScrollPane and the JScrollPane is inside a JTabbedPane called 'tabs'(instance name). tabs is on a JFrame with the class name BrowserFrame. From HTMLFrame, is there a way to get a handle to BrowserFrame without having to pass it down and without making anything static?

By the way, thanks for all your help guys.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Derek Boring:
Ok, I think I understand now. Because BrowserFrame is a class name and not an instance name, it requires a static call to it, right? ...

Yes. Consider that there might be multiple instances of BrowserFrame, or there might not be any instances at all. If you try to call an instance method from the class, how can the class "know" what instance to use?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Derek Boring:
... The code at the very top is in HTMLFrame which extends JPanel. HTMLFrame is in a JScrollPane and the JScrollPane is inside a JTabbedPane called 'tabs'(instance name). tabs is on a JFrame with the class name BrowserFrame. From HTMLFrame, is there a way to get a handle to BrowserFrame without having to pass it down and without making anything static? ...

Your HTMLFrame (a JPanel) can call getTopLevelAncestor() for a reference to the BrowserFrame (a top-level JFrame).

But you need to be a little careful, because this method will only work after the HTMLFrame has been added to the HTMLFrame. If you call it before then, the method will return null. This means you can't call it while HTMLFrame is being constructed, because it hasn't been added to anything at that point.

So you might be best off just passing the top frame to HTMLFrame's constructor...

[ August 24, 2007: Message edited by: marc weber ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by marc weber:
...Your HTMLFrame (a JPanel) can call getTopLevelAncestor() for a reference to the BrowserFrame (a top-level JFrame)...

I've never used this method before, so I wanted to make sure it worked as expected. Here is some code that demonstrates both ways.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static question
 
Similar Threads
getGraphics() returns null
abstract
Super and subclass
Static overloading
The game of statics