Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > newbie, 2 questions : variable scope and XDarwin

Reply
Thread Tools

newbie, 2 questions : variable scope and XDarwin

 
 
biner
Guest
Posts: n/a
 
      08-30-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

*****************************
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      08-30-2004
biner <(E-Mail Removed)> scribbled the following:
> 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 ;


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;

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It sure is cool having money and chicks."
- Beavis and Butt-head
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments