• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static question

 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tony Docherty
Bartender
Posts: 2965
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the method being called. It's not static.

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic