Results 1 to 9 of 9

Thread: Variables

  1. #1
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Variables

    Hello again,

    Whats the difference between class variables, instance variables, local variables, global variables ?? Could u plz provide some examples as an illustration ?

    Thanks in advance,
    Regards,
    toiletpaper

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    18,340
    Rep Power
    27

    Default Re: Variables

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Variables

    What about global variables ??

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,739
    Rep Power
    7

    Default Re: Variables

    There is no such think as a global variable in Java. The closest thing would be public static class variables.
    But I don't consider them a real global variables. Besides, they aren't needed (imho). If someone thinks they
    really need them then they should probably re-architect their app.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,063
    Rep Power
    8

    Default Re: Variables

    Quote Originally Posted by jim829 View Post
    There is no such think as a global variable in Java. The closest thing would be public static class variables.
    But I don't consider them a real global variables. Besides, they aren't needed (imho). If someone thinks they
    really need them then they should probably re-architect their app.
    +1, although constants (public static final) are still a useful thing.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Variables

    PHP Code:
    public class Die
    {
    private final int MAX = 6;
    
    .
    .
    .
    
    }
    Is this considered as a static int ? Is it a class variable ?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default Re: Variables

    Quote Originally Posted by toiletpaper View Post
    PHP Code:
    public class Die
    {
    private final int MAX = 6;
    
    .
    .
    .
    
    }
    Is this considered as a static int ? Is it a class variable ?
    I don't see the 'static' keyword anywhere, so it isn't a static variable; it is a private variable (can't be 'seen' outside the class) and it is final, so it can't be changed anywhere in the object.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  8. #8
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Variables

    What if i declared it as final int MAX = 6; it still wouldn't be considered as a class variable correct ?

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,251
    Rep Power
    4

    Default Re: Variables

    Quote Originally Posted by toiletpaper View Post
    What if i declared it as final int MAX = 6; it still wouldn't be considered as a class variable correct ?
    Yes, variables that are NOT static are NOT class variables, those are instance variables.

    Instance variables (non-static fields) are unique to each instance of a class. Class variables (static fields) are fields declared with the static modifier; there is exactly one copy of a class variable, regardless of how many times the class has been instantiated. Local variables store temporary state inside a method. Parameters are variables that provide extra information to a method; both local variables and parameters are always classified as "variables" (not "fields").
    Source: Summary of Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Edit: answered the question.
    Last edited by SurfMan; 05-08-2014 at 10:41 AM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

Similar Threads

  1. Variables, If, else
    By ElmuKelmuZ in forum New To Java
    Replies: 3
    Last Post: 10-10-2012, 01:43 AM
  2. delete variables
    By Stamoulohta in forum New To Java
    Replies: 5
    Last Post: 03-23-2011, 09:18 PM
  3. help with variables please
    By arimaliz in forum New To Java
    Replies: 3
    Last Post: 03-22-2011, 05:30 PM
  4. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  5. Variables
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 01:44 PM

Posting Permissions

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