Results 1 to 6 of 6

Thread: try. this.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103

    try. this.

    int i;

    try // in the main block
    {
    i = 0;
    this.i = i;
    }
    catch (IOException){}


    ERROR: undefined variable: this

    how do i make a variable in a try block available to other methods in the class?

    as a matter of fact, how to i make things, such as arrays, declared in a try block available outside?

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    If that code is in your main method, then that is your whole problem. The main method is static, meaning its not associated with any instance of the class. Therefore, any static method will have no concept as to what 'this' is.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103
    but how do i overcome this? not use main?

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: try. this.

    Originally posted by quipy
    how do i make a variable in a try block available to other methods in the class?

    as a matter of fact, how to i make things, such as arrays, declared in a try block available outside?
    Java has a very strict scope definition. Any variable declared within a block will only be available in that block. If you want global access, declare the variable at the class level.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by quipy
    but how do i overcome this? not use main?
    Well, that's not really an option. What you should probably do is come up with better naming conventions for your variables. That way, you won't need to worry about things like that.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Quipy, instance methods are passed an implicit parameter which is commonly refered to as this which is a reference to the object on which the method is being invoked. Since you are trying to use this in a static method it wont work. Also any static methods that are delcared within your class can only access static members of the class. Reason being because since a static method can be invoked via a class name no instance of the class might have been created.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width