Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Rudimentary java.util.prefs question

 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, sorry to be asking the simplest possible question about this API, but I am getting a complier error right off the bat and not sure why.

Java Preferences API Overview states:

The following examples illustrate how you might obtain the Preferences objects (system and user) pertaining to the enclosing class. These examples would work only inside instance methods.



It then cites:



The above example obtains per-user preferences. If a single, per-system value were desired, the first line in foo would be replaced by: Preferences prefs = Preferences.systemNodeForPackage(this);





So I added the following method to my class ViscoSimGUI:



But I immediately get the error

The method systemNodeForPackage(Class<?>) in the type Preferences is not applicable for the arguments (ViscoSimGUI)



It's definitely an instance method as the constructor is



I figure I am missing something too obvious for even the API overview, and would appreciate any help. Thanks, Eric
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The method systemNodeForPackage(Class<?>) in the type Preferences is not applicable for the arguments (ViscoSimGUI)


Hint: What do you get if you say this.getClass()?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That removes the compiler error, thank you. Also works to use ViscoSimGUI.class .

I am trying to think why -- maybe this was referring to an instance of the class when it needs to refer to the class itself here. However I still don't see why the API overview's example would work, or some other examples I see on the web.

I just tried taking off the "extends JFrame", also removing the constructor...I don't know what is different in mine.

Thanks for the help though.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eric Barnhill wrote:It then cites:


No it doesn't. The actual code is

this and Gadget.class are not the same things. this is a reference to the current instance of the Gadget class, whereas Gadget.class is an instance of the Class class.
 
Eric Barnhill
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, I was looking at an outdated overview page from a previous Java version.

This discussion helped me understand the keyword this a lot better though.

Thank you for your help.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic