Getting the Instance of a Class

I have this program where each user had there own prefences stored in there directory. A class called UserPrefences load the prefences into the data members of that class.

The problem is I want to get the instance of that prefences which was loaded to be accessed by other classes. I don't want to have to reload all the prefences again from a text file but rather get the instance of UserPrefences from different class. So how do I get the instance of that one class without creating a new object each time I access prefences from a different class?

[564 byte] By [blackmagea] at [2007-11-27 11:13:55]
# 1

Use the preferences API:

http://java.sun.com/j2se/1.4.2/docs/guide/lang/preferences.html

BigDaddyLoveHandlesa at 2007-7-29 14:03:44 > top of Java-index,Java Essentials,Java Programming...
# 2

I'm making my own prefences though, and I just want the same instance of the class. UserPrefences is a class I created.

blackmagea at 2007-7-29 14:03:44 > top of Java-index,Java Essentials,Java Programming...
# 3

As BDLH suggests, use the prefs API.

If you want to roll your own, then just make it a singleton, or, if you need multiple prefs instances, the singleton (or a static member variable) can be a map where the key is whatever you use to identify the instances and the value is each instance.

jverda at 2007-7-29 14:03:44 > top of Java-index,Java Essentials,Java Programming...
# 4

Object.clone()

works wonders.

jGardnera at 2007-7-29 14:03:44 > top of Java-index,Java Essentials,Java Programming...
# 5

Singleton?

Can there be a static class? Because there are many fields in these prefences.

And I know about the Prefences API, I just prefer to make my own due to special features I'm adding.

blackmagea at 2007-7-29 14:03:44 > top of Java-index,Java Essentials,Java Programming...
# 6

> > Object.clone()

>

?

jverda at 2007-7-29 14:03:44 > top of Java-index,Java Essentials,Java Programming...
# 7

> I'm making my own prefences though, and I just want

> the same instance of the class. UserPrefences is a

> class I created.

Your userPreferences class could be implemented using package java.util.prefs.

I admit I have no real idea what you going on about in your original post.

BigDaddyLoveHandlesa at 2007-7-29 14:03:44 > top of Java-index,Java Essentials,Java Programming...
# 8

> Singleton?

It's a design pattern you use when you want exactly one instance of something to exist.

> Can there be a static class?

Top level classes cannot be static, but you can have a class full of static member variables.

> Because there are many

> fields in these prefences.

Not sure how that's relevant.

You've given almost no information about your requirements, so I can't really give you any more specific help.

jverda at 2007-7-29 14:03:44 > top of Java-index,Java Essentials,Java Programming...
# 9

What do you mean "?"

If I want to create a copy of an object, I can implement Cloneable and go from there.

Or I can return "this" in a method.

or...

jGardnera at 2007-7-29 14:03:44 > top of Java-index,Java Essentials,Java Programming...
# 10

And I doubt that singleton is the best choice; he said that users have their own preferences.

jGardnera at 2007-7-29 14:03:44 > top of Java-index,Java Essentials,Java Programming...
# 11

I think the real problem is no one understands the original question.

Blackie,

Post some (pseudo)code or otherwise try to be clearer. Everyone is confused.

BigDaddyLoveHandlesa at 2007-7-29 14:03:44 > top of Java-index,Java Essentials,Java Programming...
# 12

Well, I think I got is just from the answers given. I know have this:

public class UserPrefences(){

private static final UserPrefences instance =new User Prefences();

private UserPrefencesr() {

}

public static UserPrefences getInstance() {

return instance;

}

-

blackmagea at 2007-7-29 14:03:44 > top of Java-index,Java Essentials,Java Programming...
# 13

> Well, I think I got is just from the answers you gave. I know have this:

That's just the code for a classic Singleton. Prob Solv?

BigDaddyLoveHandlesa at 2007-7-29 14:03:44 > top of Java-index,Java Essentials,Java Programming...
# 14

I haven't really tested it out, but if I can get data members from it, then yes.

Thnx

blackmagea at 2007-7-29 14:03:44 > top of Java-index,Java Essentials,Java Programming...
# 15

Not, it gives me a null point exception, so the object wasn't loaded...

blackmagea at 2007-7-29 14:03:49 > top of Java-index,Java Essentials,Java Programming...
# 16

> Not, it gives me a null point exception, so the

> object wasn't loaded...

Post a small (<1 page) complete example program that forum members can

copy and run that demonstrates your problem.

BigDaddyLoveHandlesa at 2007-7-29 14:03:49 > top of Java-index,Java Essentials,Java Programming...
# 17

> Well, I think I got is just from the answers given. I

> know have this:

> > public class UserPrefences(){

> private static final UserPrefences instance =new User

> Prefences();

>

> private UserPrefencesr() {

>

>}

> public static UserPrefences getInstance() {

>return instance;

> }

>

>

> -

Singleton? That's Factory, isn't it?

Edit: Ah. He is returning something static. Nevermind.

Message was edited by:

jGardner

jGardnera at 2007-7-29 14:03:49 > top of Java-index,Java Essentials,Java Programming...
# 18

> What do you mean "?"

>

> If I want to create a copy of an object, I can

> implement Cloneable and go from there.

I didn't see anyhthing that suggests that he wants a copy of an object.

>

> Or I can return "this" in a method.

That won't copy an object.

jverda at 2007-7-29 14:03:49 > top of Java-index,Java Essentials,Java Programming...
# 19

> And I doubt that singleton is the best choice; he

> said that users have their own preferences.

He hasn't really made his requirements clear at all. You could still use a singleton for multiple users though. The singleton would be a map from, say, user name to prefs.

jverda at 2007-7-29 14:03:49 > top of Java-index,Java Essentials,Java Programming...
# 20

Ok, code. For it to work you need the full thing, but only the top part matters, everythign else works

import java.awt.Color;

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.Toolkit;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.BorderFactory;

import javax.swing.JComboBox;

import javax.swing.JDialog;

import javax.swing.JLabel;

import javax.swing.JList;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import Main.MenuBar;

public class UserPrefences extends EventQueue implements MouseListener {

private static final UserPrefences instance= new UserPrefences();

static{

Toolkit.getDefaultToolkit().getSystemEventQueue().push(instance);

}

private JDialog screen;

private JComboBox runDefualtDisanceComboBox, runReturnDistanceComboBox, runLowReturnDistanceCombBox;

private JPanel topMenu, bottomMenu, item1Panel, item2Panel, item3Panel,item4Panel, runPrefPanel;

JList workoutList;

private MenuBar menu;

private JLabel generalLabel, connectionLabel, chatLabel, workOutsLabel;

private Font font;

private int width;

public UserPrefences(MenuBar bar) {

menu=bar;

setClassAttributes();

createScreen();

createTopMenu();

createBottomMenu();

createworkOutSubPanel();

screen.setVisible(true);

}//UserPrefences

private UserPrefences() {

}

public static UserPrefences getInstance(){

return instance;

}

private void createScreen() {

screen=new JDialog();

screen.setSize(width+10,500+20);

screen.setTitle("Prefences");

screen.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 10));

//screen.setResizable(false);

}//end createScreen

private void createTopMenu(){

topMenu=new JPanel();

item1Panel=new JPanel();

item2Panel=new JPanel();

item3Panel=new JPanel();

item4Panel=new JPanel();

topMenu.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));

topMenu.setPreferredSize(new Dimension(width, 100));

topMenu.setBorder(BorderFactory.createEtchedBorder());

//topMenu.setSize(width, 200);

generalLabel=new JLabel("General");

connectionLabel=new JLabel("Connection");

chatLabel=new JLabel("Chat");

workOutsLabel=new JLabel("Work-Outs");

generalLabel.setPreferredSize(new Dimension(80, 20));

connectionLabel.setPreferredSize(new Dimension(80, 20));

chatLabel.setPreferredSize(new Dimension(80, 20));

workOutsLabel.setPreferredSize(new Dimension(80, 20));

item1Panel.add(generalLabel);

item2Panel.add(workOutsLabel);

item3Panel.add(chatLabel);

item4Panel.add(connectionLabel);

item1Panel.addMouseListener(this);

item2Panel.addMouseListener(this);

item3Panel.addMouseListener(this);

item4Panel.addMouseListener(this);

item1Panel.setPreferredSize(new Dimension(120, 90));

item2Panel.setPreferredSize(new Dimension(120, 90));

item3Panel.setPreferredSize(new Dimension(120, 90));

item4Panel.setPreferredSize(new Dimension(120, 90));

item1Panel.setBorder(BorderFactory.createEtchedBorder());

item2Panel.setBorder(BorderFactory.createEtchedBorder());

item3Panel.setBorder(BorderFactory.createEtchedBorder());

item4Panel.setBorder(BorderFactory.createEtchedBorder());

topMenu.add(item1Panel);

topMenu.add(item2Panel);

topMenu.add(item3Panel);

topMenu.add(item4Panel);

screen.getContentPane().add(topMenu);

}

private void createBottomMenu(){

bottomMenu=new JPanel();

bottomMenu.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));

bottomMenu.setPreferredSize(new Dimension(width, 350));

bottomMenu.setBorder(BorderFactory.createEtchedBorder());

screen.getContentPane().add(bottomMenu);

}

private void setClassAttributes() {

width=500;

font=new Font("Times", Font.PLAIN, 12);

}//setClassAttributes

private void createworkOutSubPanel(){

String[] workouts={"Runnings","Weights","Swimming"};

String[] distances={"yards","feet","meters","miles","kilometers"};

workoutList=new JList(workouts);

workoutList.setPreferredSize(new Dimension(100, 350));

workoutList.addMouseListener(this);

workoutList.setBorder(BorderFactory.createEtchedBorder());

runPrefPanel=new JPanel();

runPrefPanel.setLayout(null);

runPrefPanel.setPreferredSize(new Dimension(390, 350));

runDefualtDisanceComboBox=new JComboBox(distances);

runReturnDistanceComboBox=new JComboBox(distances);

runLowReturnDistanceCombBox=new JComboBox(distances);

JTextArea runDefaultLabel=new JTextArea();

JTextArea runReturnLabel=new JTextArea();

JTextArea runDeReturn2Label=new JTextArea();

runDefualtDisanceComboBox.setBounds(20, 50, 100, 25);

runDefualtDisanceComboBox.setSelectedIndex(2);

runReturnDistanceComboBox.setBounds(20, 110, 100, 25);

runLowReturnDistanceCombBox.setBounds(20, 180, 100, 25);

runDefaultLabel.setText("Default Distance:\nSets initial distances in the running log to the" +

" selected distance");

runDefaultLabel.setLineWrap(true);

runDefaultLabel.setEditable(false);

runDefaultLabel.setFont(font);

runDefaultLabel.setBounds(20, 20, 320, 40);

runReturnLabel.setText("Calculate Distance:\nReturns distances calculated to the selected" +

" value" );

runReturnLabel.setLineWrap(true);

runReturnLabel.setEditable(false);

runReturnLabel.setFont(font);

runReturnLabel.setBounds(20, 80, 320, 40);

runDeReturn2Label.setText("Default Calculate:\n If the units of measurment returned is less that 1" +

"unit of the calculated distance, return distance as selected");

runDeReturn2Label.setLineWrap(true);

runDeReturn2Label.setEditable(false);

runDeReturn2Label.setFont(font);

runDeReturn2Label.setBounds(20, 150, 320, 40);

runPrefPanel.add(runDefualtDisanceComboBox);

runPrefPanel.add(runReturnDistanceComboBox);

runPrefPanel.add(runLowReturnDistanceCombBox);

runPrefPanel.add(runDefaultLabel);

runPrefPanel.add(runReturnLabel);

runPrefPanel.add(runDeReturn2Label);

}

public void mouseClicked(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}//end actionperformed

public void mouseExited(MouseEvent arg0) {

// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {

// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent e) {

if(e.getSource().equals(item1Panel)){

item1Panel.setBackground(Color.BLUE);

item2Panel.setBackground(Color.WHITE);

item3Panel.setBackground(Color.WHITE);

item4Panel.setBackground(Color.WHITE);

}

else if(e.getSource().equals(item2Panel)){

item1Panel.setBackground(Color.WHITE);

item2Panel.setBackground(Color.BLUE);

item3Panel.setBackground(Color.WHITE);

item4Panel.setBackground(Color.WHITE);

bottomMenu.removeAll();

bottomMenu.add(workoutList);

bottomMenu.add(runPrefPanel);

bottomMenu.setVisible(false);

bottomMenu.setVisible(true);

}

else if(e.getSource().equals(item3Panel)){

item1Panel.setBackground(Color.WHITE);

item2Panel.setBackground(Color.WHITE);

item3Panel.setBackground(Color.BLUE);

item4Panel.setBackground(Color.WHITE);

}

else if(e.getSource().equals(item4Panel)){

item1Panel.setBackground(Color.WHITE);

item2Panel.setBackground(Color.WHITE);

item3Panel.setBackground(Color.WHITE);

item4Panel.setBackground(Color.BLUE);

}

}

public JComboBox getRunDefault(){

return runDefualtDisanceComboBox;

}

}

blackmagea at 2007-7-29 14:03:49 > top of Java-index,Java Essentials,Java Programming...
# 21

Too much code. I'm sure most of that is irrelevant to your question.

BigDaddyLoveHandlesa at 2007-7-29 14:03:49 > top of Java-index,Java Essentials,Java Programming...
# 22

You shouldn't need that much code to demonstrate your problem. And you neglected to say exactly what the problem is and exactly where it's occurring.

jverda at 2007-7-29 14:03:49 > top of Java-index,Java Essentials,Java Programming...
# 23

For the requirement:

This class is going to load a textfile with user prefences in a folder, but I only want it to load once, and thats when they log in. And then I want to be able to get the data members from that class/object in other classes without creating a new UserPrefences();

blackmagea at 2007-7-29 14:03:49 > top of Java-index,Java Essentials,Java Programming...
# 24

> For the requirement:

>

> This class is going to load a textfile with user

> prefences in a folder, but I only want it to load

> once, and thats when they log in. And then I want to

> be able to get the data members from that

> class/object in other classes without creating a new

> UserPrefences();

So load it once and store it in a member variable that can be accessed directly or indirectly andlives as long as the program.

What part are you having trouble with?

jverda at 2007-7-29 14:03:49 > top of Java-index,Java Essentials,Java Programming...
# 25

Null pointer.

When I try to get the data members of UserPrefences from a different class without creating a new UserPrefences Object, I get a null point exception.

blackmagea at 2007-7-29 14:03:49 > top of Java-index,Java Essentials,Java Programming...
# 26

Ok, here is the code shorted

public class UserPrefences extends EventQueue implements MouseListener {

private static final UserPrefences instance= new UserPrefences();

static{

Toolkit.getDefaultToolkit().getSystemEventQueue().push(instance);

}

private JDialog screen;

private JComboBox runDefualtDisanceComboBox, runReturnDistanceComboBox, runLowReturnDistanceCombBox;

private JPanel topMenu, bottomMenu, item1Panel, item2Panel, item3Panel,item4Panel, runPrefPanel;

JList workoutList;

private MenuBar menu;

private JLabel generalLabel, connectionLabel, chatLabel, workOutsLabel;

private Font font;

private int width;

public UserPrefences(MenuBar bar) {

createworkOutSubPanel();

}//UserPrefences

private UserPrefences() {

}

public static UserPrefences getInstance(){

return instance;

}

private void createworkOutSubPanel(){

String[] workouts={"Runnings","Weights","Swimming"};

String[] distances={"yards","feet","meters","miles","kilometers"};

runDefualtDisanceComboBox=new JComboBox(distances);

runReturnDistanceComboBox=new JComboBox(distances);

runLowReturnDistanceCombBox=new JComboBox(distances);

}

public JComboBox getRunDefault(){

return runDefualtDisanceComboBox;

}

}

Message was edited by:

blackmage

blackmagea at 2007-7-29 14:03:49 > top of Java-index,Java Essentials,Java Programming...
# 27

And the error occurs, NullPointer, when using

public JComboBox getRunDefault(){

return runDefualtDisanceComboBox;

}

blackmagea at 2007-7-29 14:03:49 > top of Java-index,Java Essentials,Java Programming...
# 28

I get a syntax error:

UserPrefences is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener

public class UserPrefences extends EventQueue implements MouseListener {

Can you post a short yet complete example program?

Message was edited by:

BigDaddyLoveHandles

BigDaddyLoveHandlesa at 2007-7-29 14:03:49 > top of Java-index,Java Essentials,Java Programming...
# 29

> And the error occurs, NullPointer, when using

>

> > public JComboBox getRunDefault(){

> return runDefualtDisanceComboBox;

> }

>

Your class has two constructors and only one initializes runDefualtDisanceComboBox.

Guess which constructor the code you posted uses?

BigDaddyLoveHandlesa at 2007-7-29 14:03:49 > top of Java-index,Java Essentials,Java Programming...
# 30

Ok, this one is short with main method includes

import java.awt.Color;

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.Toolkit;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.BorderFactory;

import javax.swing.JComboBox;

import javax.swing.JDialog;

import javax.swing.JLabel;

import javax.swing.JList;

import javax.swing.JPanel;

import javax.swing.JTextArea;

public class UserPrefences extends EventQueue {

private static final UserPrefences instance= new UserPrefences();

static{

Toolkit.getDefaultToolkit().getSystemEventQueue().push(instance);

}

private JComboBox runDefualtDisanceComboBox;

private int width;

//Constructor that is initially accessed

public UserPrefences(String string) {

createworkOutSubPanel();

}//UserPrefences

//Access By the Instance

private UserPrefences() {

}

//Gets the Instances in Another Class

public static UserPrefences getInstance(){

return instance;

}

private void createworkOutSubPanel(){

String[] distances={"yards","feet","meters","miles","kilometers"};

runDefualtDisanceComboBox=new JComboBox(distances);

runDefualtDisanceComboBox.setSelectedIndex(2);

System.out.println("Run Default Created and Set Selected");

}

public JComboBox getRunDefault(){

return runDefualtDisanceComboBox;

}

public static void main(String[] args){

UserPrefences user=new UserPrefences("go");

System.out.println(user.getRunDefault().getSelectedItem().toString());

UserPrefences user2=UserPrefences.getInstance();

System.out.println(user2.getRunDefault().getSelectedItem().toString());

}

}

blackmagea at 2007-7-29 14:03:53 > top of Java-index,Java Essentials,Java Programming...
# 31

> And the error occurs, NullPointer, when using

>

> > public JComboBox getRunDefault(){

> return runDefualtDisanceComboBox;

> }

>

That's not very helpful.

Executing the single line of code in that method cannot give NPE. So the problem must come when you try to CALL that method. So you'd have to point out which line is calling that method and giving the problem.

jverda at 2007-7-29 14:03:53 > top of Java-index,Java Essentials,Java Programming...
# 32

> > And the error occurs, NullPointer, when using

> >

> > > > public JComboBox getRunDefault(){

> > return runDefualtDisanceComboBox;

> > }

> >

>

> Your class has two constructors and only one

> initializes runDefualtDisanceComboBox.

> Guess which constructor the code you posted uses?

But because its a data member, should it be usable by the second constructor?

blackmagea at 2007-7-29 14:03:53 > top of Java-index,Java Essentials,Java Programming...
# 33

> > And the error occurs, NullPointer, when using

> >

> > > > public JComboBox getRunDefault(){

> > return runDefualtDisanceComboBox;

> > }

> >

>

> That's not very helpful.

>

> Executing the single line of code in that method

> cannot give NPE. So the problem must come when you

> try to CALL that method. So you'd have to point out

> which line is calling that method and giving the

> problem.

Look in my example code above. The first time I call the method, it works, the second time it doesn't.

blackmagea at 2007-7-29 14:03:53 > top of Java-index,Java Essentials,Java Programming...
# 34

> But because its a data member, should it be usable by

> the second constructor?

I don't know what you mean here, but what BDLH is saying is that you're never setting the value of that member.

jverda at 2007-7-29 14:03:54 > top of Java-index,Java Essentials,Java Programming...
# 35

This is in regards to the code in reply #30:

Field runDefualtDisanceComboBox is what is causing the NPE.

The only place in your code it is given a non-null value is by method createworkOutSubPanel.

The only place that method is called is by the first constructor (the one taking a String).

You create two instances of UserPreferences, user1 amd user2. The first uses

UserPrefences(String), and hence has a non-null runDefualtDisanceComboBox ,

while user2 uses UserPrefences(), and hence has a null runDefualtDisanceComboBox.

That null field causes a NPE when getSelectedItem is applied to null. Line 66.

There's nothing tricky going on here, just a lapse in logic.

BigDaddyLoveHandlesa at 2007-7-29 14:03:54 > top of Java-index,Java Essentials,Java Programming...
# 36

So I can only use one constructor with getInstance()?

blackmagea at 2007-7-29 14:03:54 > top of Java-index,Java Essentials,Java Programming...
# 37

> So I can only use one constructor with getInstance()?

I can't answer that question. You need to stop and think about your code.

Why isn't the no-argument constructor initializing that field?

Why do you need two constructors in a Singleton class?

Why are you subclassing EventQueue? That just seems weird to me.

BigDaddyLoveHandlesa at 2007-7-29 14:03:54 > top of Java-index,Java Essentials,Java Programming...
# 38

> I can't answer that question. You need to stop and

> think about your code.

> Why isn't the no-argument constructor initializing

> that field?

Because I thought the first constructor with an arguement initilized the varable and set the data members. And I thought the second constructor would have acess to same data members. Thas what I am trying to do.

blackmagea at 2007-7-29 14:03:54 > top of Java-index,Java Essentials,Java Programming...
# 39

> And I thought the second constructor would have acess to same data members.

If you create two instances of a class, they get separate copies of instance fields.

That's pretty basic.

BigDaddyLoveHandlesa at 2007-7-29 14:03:54 > top of Java-index,Java Essentials,Java Programming...
# 40

When I switched it to one constructor, it initalizes the data members all over again and thats what I'm trying not to do. Instantiate the object one and call that one instance in different classes.

blackmagea at 2007-7-29 14:03:54 > top of Java-index,Java Essentials,Java Programming...
# 41

Do you only want to create one instance of the class? If so, what was wrong with the Singleton Patterm?

BigDaddyLoveHandlesa at 2007-7-29 14:03:54 > top of Java-index,Java Essentials,Java Programming...
# 42

What I was doing before wasn't the single pattern?

blackmagea at 2007-7-29 14:03:54 > top of Java-index,Java Essentials,Java Programming...
# 43

> What I was doing before wasn't the single pattern?

I'm not sure which "before" you are referring to, but I think it was a one

with two constructors. Try achieving a Singleton with *one* constructor

defined in your class and make sure that one constructor's leaves the object

properly defined -- no null fields that later blow up.

BigDaddyLoveHandlesa at 2007-7-29 14:03:54 > top of Java-index,Java Essentials,Java Programming...
# 44

Ok, I tried this, and its going to recreate every variable twice:

import java.awt.Color;

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.Toolkit;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.BorderFactory;

import javax.swing.JComboBox;

import javax.swing.JDialog;

import javax.swing.JLabel;

import javax.swing.JList;

import javax.swing.JPanel;

import javax.swing.JTextArea;

public class UserPrefences extends EventQueue {

private static final UserPrefences instance= new UserPrefences("Go");

boolean firstinstance=true;

static{

Toolkit.getDefaultToolkit().getSystemEventQueue().push(instance);

}

private JComboBox runDefualtDisanceComboBox;

//Constructor that is initially accessed

public UserPrefences(String string) {

if(firstinstance==true){

createworkOutSubPanel();

firstinstance=false;

System.out.println("First Instance");

}

else{

System.out.println("Second Instance");

}

}//UserPrefences

//Access By the Instance

private UserPrefences() {

}

//Gets the Instances in Another Class

public static UserPrefences getInstance(){

return instance;

}

private void createworkOutSubPanel(){

String[] distances={"yards","feet","meters","miles","kilometers"};

runDefualtDisanceComboBox=new JComboBox(distances);

runDefualtDisanceComboBox.setSelectedIndex(2);

System.out.println("Run Default Created and Set Selected");

}

public JComboBox getRunDefault(){

return runDefualtDisanceComboBox;

}

public static void main(String[] args){

UserPrefences user=new UserPrefences("GO");

System.out.println(user.getRunDefault().getSelectedItem().toString());

UserPrefences user2=UserPrefences.getInstance();

System.out.println(user2.getRunDefault().getSelectedItem().toString());

}

}

blackmagea at 2007-7-29 14:03:54 > top of Java-index,Java Essentials,Java Programming...
# 45

And then I turn the boolean static and still got a null.

blackmagea at 2007-7-29 14:03:58 > top of Java-index,Java Essentials,Java Programming...
# 46

> Ok, I tried this, and its going to recreate every variable twice:

That code has problems, but I think you need to step back

and be clearer about your requirements. Just how many instances of this

class do you want to create? One second you're saying Singleton

and the next you're doing stuff like this.

BigDaddyLoveHandlesa at 2007-7-29 14:03:58 > top of Java-index,Java Essentials,Java Programming...
# 47

I just want to create on, and I thought this was a Singleton. So really, I'm lost. Is there any example code of a single instance that is used over and over again?

blackmagea at 2007-7-29 14:03:58 > top of Java-index,Java Essentials,Java Programming...
# 48

There's basically nothing to Singleton: just use it:

public class Singleton {

private static Singleton instance = new Singleton();

private int count;

private Singleton() {}

public static Singleton getInstance() {return instance;}

public void method() {

count++;

System.out.println("method is being called " + count + " time(s)");

}

}

public class SingletonClient {

public static void main(String[] args) {

for(int i = 0; i < 10; ++i) {

test();

}

}

static void test() {

Singleton.getInstance().method();

}

}

I'm not claiming that Singleton is the answer to your problem, since you have never defined your problem...

BigDaddyLoveHandlesa at 2007-7-29 14:03:58 > top of Java-index,Java Essentials,Java Programming...
# 49

Got it. I changed from

UserPrefences user=new UserPrefences("GO");

System.out.println(user.getRunDefault().getSelectedItem().toString());

UserPrefences user2=UserPrefences.getInstance();

System.out.println(user2.getRunDefault().getSelectedItem().toString());

to

UserPrefences user=UserPrefences.getInstance();

System.out.println(user.getRunDefault().getSelectedItem().toString());

UserPrefences user2=UserPrefences.getInstance();

System.out.println(user2.getRunDefault().getSelectedItem().toString());

[/code]

And it works. Thnx!

blackmagea at 2007-7-29 14:03:58 > top of Java-index,Java Essentials,Java Programming...
# 50

> > Well, I think I got is just from the answers given.

> I

> > know have this:

> > > > public class UserPrefences(){

> > private static final UserPrefences instance =new

> User

> > Prefences();

> >

> > private UserPrefencesr() {

> >

> >}

> > public static UserPrefences getInstance() {

> >return instance;

> > }

> >

> >

> > -

>

> Singleton? That's Factory, isn't it?

>

> Edit: Ah. He is returning something static.

> Nevermind.

>

> Message was edited by:

> jGardner

It's a singleton implemented with a (totally pointless) factory method

georgemca at 2007-7-29 14:03:58 > top of Java-index,Java Essentials,Java Programming...
# 51

Blackmage, your code makes no sense. You say it's a user preferences class, but it extends EventQueue, implements MouseListener and does a bunch of other UI stuff. Wholly inappropriate, if you ask me. A preferences class has nothing to do with GUIs, whatsoever. I'm not sure extending EventQueue is a good idea in any case, let alone this one

By having this class do so much, not only is it conceptually wrong, it means you yourself are not focusing on your actual problem. The problems you're having may very well be nothing at all to do with preferences. Start again, and concentrate on just the preferences. It shouldn't depend on a UI for that

georgemca at 2007-7-29 14:03:58 > top of Java-index,Java Essentials,Java Programming...
# 52

Wild guess but I am guessing that the correct answer is here....

http://www.catb.org/~esr/faqs/smart-questions.html

jschella at 2007-7-29 14:03:58 > top of Java-index,Java Essentials,Java Programming...