interference with constructor

Hey, is it possible to create an instance of a class without calling its constructor automatically?
The way im doing it now:
test = new Test();
Thanks for any help.
[191 byte] By [Trainwreck] at [2007-11-20 11:57:15]
# 1 Re: interference with constructor
Yes, using reflection ( http://java.sun.com/docs/books/tutorial/reflect/index.html) and the Constructor.newInstance ( http://java.sun.com/javase/6/docs/api/java/lang/reflect/Constructor.html#newInstance(java.lang.Object...)) function.

- petter
wildfrog at 2007-11-10 2:13:57 >
# 2 Re: interference with constructor
Thanks, but i cant get it to work with that. Im probably doing something wrong, just cant find a good clear example.

What im trying to do so adding a button to an actionlistener. The button is Build.java and the actionlistener is in Actions.java. I could do it like this:

Actions actions;
actions = new Actions();
startButton.addActionlistener(actions);

But that way im also calling the constructor in Actions.java, and i want to avoid that...
Trainwreck at 2007-11-10 2:14:57 >
# 3 Re: interference with constructor
Could you please explain why you don't want to construct the object? Using reflection is just another (complicated and inefficient) way to call the constructor.

- petter
wildfrog at 2007-11-10 2:15:56 >
# 4 Re: interference with constructor
Very useful when you need it, but not something to do unless you really have to - which is generally pretty rare. Apart from potentially being much slower and less easy to follow than standard constructor calls, it also has the big drawback that it makes it very difficult to find where particular classes are being created in the source code of non-trivial applications.

There are features that should not be used. There are concepts that should not be exploited. There are problems that should not be solved. There are programs that should not be written...
R. Harter
dlorde at 2007-11-10 2:17:01 >
# 5 Re: interference with constructor
Its kinda hard to tell, but i'll give it a try.

I have three classes, Main.java, Build.java and Actions.java.

In Build.java i have a few functions that creates buttons and labels on my GUI. These functions are called from Main.java.

So basically my Main.java looks kinda like this:

public static void main(String[] args) {

jpanel = new JPanel();
jpanel.setLayout(null);

build = new Build();
build.startKnop();

jframe = new JFrame();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setResizable(false);
jframe.setSize(350,400);
jframe.add(jpanel);
jframe.setVisible(true);

}

So im creating an instance of Build.java so i can call the functions from Main.java that are located in Build.java.
This way the constructor in Build.java also gets called, but that fine.

Like i said before, in Build.java i have functions which creates buttons and stuff. But i also have to add an ActionListener to the button.

I can do that like this:

public void startKnop()
{
startKnop = new JButton();
startKnop.setBounds(224,270,100,20);
startKnop.setText("Start");
Main.jpanel.add(startKnop);

actions = new Actions();
startKnop.addActionListener(actions);
}

So this way the constructor is called in Actions.java, which is still fine.

So i have the ActionListener in Actions.java. Now when i press on the button "startKnop" then it gets successfully triggered in Actions.java and a function is called which is located in Build.java. I have this code for it:

public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(Build.startKnop))
{
build = new Build();
build.DraaiStroken();
}
}

As you can see i have to create a new instance again of Build.java or else im not able to call the function located in Build. This means that everything in the constructor in Build.java is called Again, and all my previous declared variables are reset in Build.

So thats the whole problem im having.
Trainwreck at 2007-11-10 2:17:55 >
# 6 Re: interference with constructor
Well, what you probably want to do is to pass an instance of 'Build' into the Actions class, and use that instance when calling DraaiStroken. Something like:

class Actions /* implements interfaces etc. */
{
private Build build;

public Actions(Build build)
{
this.build = build;
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(Build.startKnop))
{
this.build.DraaiStroken();
}
}
}

And you Build class/startKnop function would contain something like:

Actions actions = new Actions(this); // pass 'this' Build instance as argument
startKnop.addActionListener(actions);

- petter
wildfrog at 2007-11-10 2:18:59 >
# 7 Re: interference with constructor
Great, thanks for your help! :)
Trainwreck at 2007-11-10 2:19:57 >