newbie, 2 questions : variable scope and XDarwin

Discussion in 'Java' started by biner, Aug 30, 2004.

  1. biner

    biner Guest

    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


    *****************************
     
    biner, Aug 30, 2004
    #1
    1. Advertising

  2. biner <> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "It sure is cool having money and chicks."
    - Beavis and Butt-head
     
    Joona I Palaste, Aug 30, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Walls
    Replies:
    3
    Views:
    1,074
    Colin Walls
    Oct 10, 2004
  2. RN

    Newbie - Variable Scope

    RN, Nov 17, 2003, in forum: Python
    Replies:
    3
    Views:
    309
    Duncan Booth
    Nov 17, 2003
  3. Steven T. Hatton
    Replies:
    9
    Views:
    510
  4. David Filmer
    Replies:
    19
    Views:
    267
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    208
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page