Results 1 to 8 of 8

Thread: variable

  1. #1
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

    Default variable

    Java Code:
    public class Point {
        public int x = 0;
        public int y = 0;
        // a constructor!
        public Point(int a, int b) {
        x = a;
        y = b;
        }
    }

    Hi there,

    The above program can be compiled successfully.
    But I wonder why the variable x and y at line 6 and 7 can be declared without the variable type?
    is it because it referring to line 2 and 3?

    Thanks,

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,848
    Rep Power
    5

    Default Re: variable

    of course.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

    Default Re: variable

    Gimbal,

    Thanks a lot.
    But i think it should be a local varible as it located in the constructor.
    and should be destroied after exist the block of constructor/method.
    but why it is refered to the line 2 and 3?

    pls correct me if wrong.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: variable

    If they were defined as 'int x' and 'int y' then yes, they would be local variables and they would go out of scope.
    In this case, though, they are attributes of a Point. So they don't go out of scope.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: variable

    P.S. lines 6 an 7 are not "declaring" variables, they are assigning values to already declared variables. Variable that where declared at an instance scope.

  6. #6
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

    Default Re: variable

    Tolls and Masijade,

    Well noted and thanks very much for your kind advise.



    To Masijade,

    Is it means that the "already declared variables at an instance scope" can be appeared any where inside its class?
    Last edited by wayenng; 09-26-2013 at 02:14 PM.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,848
    Rep Power
    5

    Default Re: variable

    Can be used in any method of the class, yes. Except in static methods.

    Calling them variables is not really descriptive; I call them class attributes which when I was in school was the proper generic Object Oriented programming term for them; since then I think the terminology has gone through a few changes. A class should represent something "real". Say we have a class called Person, then attributes of that class can be a name, a social security number, a birth date, etc.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 01-09-2013, 08:10 PM
  2. How to compare variable with multiple variable?
    By mharckz009 in forum New To Java
    Replies: 5
    Last Post: 09-11-2012, 04:33 PM
  3. Replies: 1
    Last Post: 04-21-2012, 06:03 PM
  4. Dynamic variable name based on other variable
    By nadissen in forum Eclipse
    Replies: 4
    Last Post: 05-06-2011, 06:22 PM
  5. Replies: 11
    Last Post: 07-07-2010, 06:02 AM

Posting Permissions

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