newbie, 2 questions : variable scope and XDarwin

B

biner

Hello,

I have two questions. One about variable scope and the other regards
using java on xdarwin.

I would like to pass a variable to an object of a given class with
the constructor and I would like that variable to be visible by the
methods of that class. To illustrate, here is the output I get from
the code that follows :

*** begin output ***
[sbiner:~/CODE/java/src] biner% java testVariableScope
const maClase, toto=10.0
const maClase, toto2=10.0
montre Toto, toto=0.0
montre Toto, toto2=10.0
*** end output ***

*** begin code ***
class maClasse {

double toto ;
double toto2 ;

// constructeur
maClasse(double toto) {
toto2=toto ;
System.out.println("const maClase, toto="+toto) ;
System.out.println("const maClase, toto2="+toto2) ;
}
// methode montre Toto
void montreToto() {
System.out.println("montre Toto, toto="+toto) ;
System.out.println("montre Toto, toto2="+toto2) ;
}
}

public class testVariableScope {

// main
public static void main(String[] args) {
double toto = 10. ;
maClasse titi = new maClasse(toto) ;
titi.montreToto() ;
}

}
*** end code ***

I would like the montreToto method to "see" the right value of toto
without having to copy toto in toto2. And I don't understand why toto2
is "viewed" and not toto. Anybody can give me a pointer ?

The other question has to do with XDarwin. Using java on Xdarwin, I
would like for the windows oppened by java (and the appletviewer) to
be opened in X and not in OSX. Is there a way to do this? It is a bit
of a pain to constantly change between XDarwin for programming and
compiling and OSX to see the results.

Any help with these question would be appreciated.

Thank you

Sebastien
(e-mail address removed)

*****************************
 
J

Joona I Palaste

biner said:
I have two questions. One about variable scope and the other regards
using java on xdarwin.
I would like to pass a variable to an object of a given class with
the constructor and I would like that variable to be visible by the
methods of that class. To illustrate, here is the output I get from
the code that follows :
*** begin output ***
[sbiner:~/CODE/java/src] biner% java testVariableScope
const maClase, toto=10.0
const maClase, toto2=10.0
montre Toto, toto=0.0
montre Toto, toto2=10.0
*** end output ***
*** begin code ***
class maClasse {
double toto ;

Instance-scope variable toto.
double toto2 ;

// constructeur
maClasse(double toto) {
toto2=toto ;

Method-scope variable toto, overrides instance-scope variable toto.
Two different variables.
System.out.println("const maClase, toto="+toto) ;
System.out.println("const maClase, toto2="+toto2) ;
}
// methode montre Toto
void montreToto() {
System.out.println("montre Toto, toto="+toto) ;
System.out.println("montre Toto, toto2="+toto2) ;
}
}
public class testVariableScope {
// main
public static void main(String[] args) {
double toto = 10. ;
maClasse titi = new maClasse(toto) ;
titi.montreToto() ;
}
}
*** end code ***
I would like the montreToto method to "see" the right value of toto
without having to copy toto in toto2. And I don't understand why toto2
is "viewed" and not toto. Anybody can give me a pointer ?

Because you have two different variables named toto. One is instance-
scope and the other is method-scope. The fact that they have the same
name doesn't mean they are the same variable. If you want the parameter
toto to be copied to your instance, use this:
this.toto = toto;
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top