NetBeans and argument of constructor

M

Mariano

I've this class, Gestisci.java, when I initialize this class from
Crea.java class I need to pass an argument to the constructor so code
in class Crea.java will be something like that:
new Gestisci(myStringVariable).setVisible(true)

=======================================
//File: Gestisci.java
public class Gestisci extends javax.swing.JFrame {

private String tipo; // declare tipo

public Gestisci(String t) { // accept string
this.tipo = t; // "bind"
}
// START A METHOD
System.out.println(tipo);
// END METHOD

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Gestisci(???).setVisible(true); // AND HERE???
}
});
}

=======================================

Now my knowledge stop here, but, what I've to put in main at place of
'???' to correctly compile?
 
L

Lew

Mariano said:
I've this class, Gestisci.java, when I initialize this class from
Crea.java class I need to pass an argument to the constructor so code
in class Crea.java will be something like that:
new Gestisci(myStringVariable).setVisible(true)

=======================================
//File: Gestisci.java
public class Gestisci extends javax.swing.JFrame {

private String tipo; // declare tipo

public Gestisci(String t) { // accept string
this.tipo = t; // "bind"
}
// START A METHOD
System.out.println(tipo);

This line will not compile. Statements need to be inside initializer blocks,
constructors or methods, not dangling "loose" as this one is.
// END METHOD

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Gestisci(???).setVisible(true); // AND HERE???
}
});
}

=======================================

Now my knowledge stop here, but, what I've to put in main at place of
'???' to correctly compile?

Whatever String you wish the Gestisci class to hold in its 'tipo' member.
What does the logic of your problem (that you are trying to solve) dictate?
In other words, what is the information that you want the Gestisci instance to
remember?
 
E

Eric Sosman

Mariano said:
I've this class, Gestisci.java, when I initialize this class from
Crea.java class I need to pass an argument to the constructor so code
in class Crea.java will be something like that:
new Gestisci(myStringVariable).setVisible(true)

=======================================
//File: Gestisci.java
public class Gestisci extends javax.swing.JFrame {

private String tipo; // declare tipo

public Gestisci(String t) { // accept string
this.tipo = t; // "bind"
}
// START A METHOD
System.out.println(tipo);
// END METHOD

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Gestisci(???).setVisible(true); // AND HERE???
}
});
}

=======================================

Now my knowledge stop here, but, what I've to put in main at place of
'???' to correctly compile?

A String (or null). The value you supply in main will
be known as `t' in the Gestisci constructor, and will become
the initial value of `tipo' in the new Gestisci object.
 
M

Mark Space

Mariano said:
I've this class, Gestisci.java, when I initialize this class from
Crea.java class I need to pass an argument to the constructor so code
in class Crea.java will be something like that:
new Gestisci(myStringVariable).setVisible(true)

=======================================
//File: Gestisci.java
public class Gestisci extends javax.swing.JFrame {

private String tipo; // declare tipo

public Gestisci(String t) { // accept string
this.tipo = t; // "bind"
System.out.println(tipo); // MUST BE INSIDE A METHOD
> } // CHANGED THIS
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Gestisci("tipo test").setVisible(true); // CHANGED HERE
}
});
}

=======================================

Now my knowledge stop here, but, what I've to put in main at place of
'???' to correctly compile?

I made some changes above. Please see the changed code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top