Results 1 to 23 of 23

Thread: global variable

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2003
    Posts
    108

    global variable

    in Java, how do we declare a global variable? All classes can use this variable.



    Thanks.

  2. #2
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: global variable

    Isnt that bad programming? I thought classes were to be independant of each other..?

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: global variable

    Java is pure Object Oriented Language, there is nothing bad you can find in Java

    To do this Mark the variable "public static" so you can access it from any other class
    just type the "name of the Containing class".variableName to access it
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink Re: global variable

    Quote Originally Posted by ComputerJy
    Java is pure Object Oriented Language, there is nothing bad you can find in Java

    To do this Mark the variable "public static" so you can access it from any other class
    just type the "name of the Containing class".variableName to access it

    Ya it's true. How can you explain following two lines,

    static a = 10;
    public static a = 10;

    please help me on this. I'm confucing
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: global variable

    public vars can be accessed from any where
    If your var isn't "private, protected or public" it's like "Friend (internal)" in .net you can only access it from the same package.
    So if you don't have a package declared you can access it from anywhere (public)
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: global variable

    OK

    Thanks
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  7. #7
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: global variable

    Quote Originally Posted by |2eM!x
    Isnt that bad programming? I thought classes were to be independant of each other..?
    It depends on what you're talking about. What you're thinking of is using getters and setters for a variable in another class, which is good OO practice. But if you have a class for each state in the United States and each state needs to have access to the president, then you need someway of sharing. Since their's only one president and it will be shared among those 50 classes, then you can make it static. Static variables and classes are only initialized once through the running.

    Hope that made sense

  8. #8
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: global variable

    Quote Originally Posted by ComputerJy
    Java is pure Object Oriented Language
    That would be incorrect due to the fact that there are primative types, look it up.

  9. #9
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: global variable

    Quote Originally Posted by vagabon
    That would be incorrect due to the fact that there are primative types, look it up.
    http://answers.yahoo.com/question/in...5114209AABH0HP
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  10. #10
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: global variable

    Quote Originally Posted by vagabon
    That would be incorrect due to the fact that there are primative types, look it up.
    What????

    *You're wrong*

  11. #11
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: global variable

    Quote Originally Posted by System_Error
    What????

    *You're wrong*
    I know, that's why I looked it up

    EDIT: Actually, he has a good point http://forum.java.sun.com/thread.jsp...71077&tstart=0
    Last edited by ComputerJy; Oct 10th, 2006 at 03:24 PM.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  12. #12
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: global variable

    primatives are not objects in the sense of oop. w00t! I knew something for once! :P
    But it doesn't matter, Java is oop enough, it doesn't really effect anything, nothing to get upset over (some people do!).
    Last edited by vagabon; Oct 11th, 2006 at 09:33 AM.

  13. #13
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: global variable

    I would have thought just the opposite I know some people that would prefer there be no primatives, but me likes.

  14. #14
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: global variable

    By the way, I guess my assumption that you were wrong is wrong

  15. #15
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: global variable

    Quote Originally Posted by System_Error
    By the way, I guess my assumption that you were wrong is wrong
    lol
    I think the primiatives make it simpler and turning them into objects is just wasted time. If someone really wants to they can use the Integer, Character, Double, etc instead, they are objects.

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: global variable

    Since Java5 and autoboxing, it's fine. Before that, primitives were a pain.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  17. #17
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: global variable

    A pain because some things such as Collections need a reference or another reason? I like the addition of generics, but I never found primatives to be annoying...... Except for cases in early versions that needed 'weird' casts to it's object class.

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: global variable

    Because of the collections, yes. And some cases where you needed a nullable value.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  19. #19
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink Re: global variable

    Quote Originally Posted by CornedBee
    Since Java5 and autoboxing, it's fine. Before that, primitives were a pain.

    Ya it's true. When i'm using Java5 feel free to used primitives.

    But i'm confusing with this,

    "Because of the collections, yes. And some cases where you needed a nullable value."

    Can you explain it little more.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  20. #20
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink Re: global variable

    Quote Originally Posted by vagabon
    primatives are not objects in the sense of oop.

    What you mean of this.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  21. #21
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: global variable

    Quote Originally Posted by eranga262154
    What you mean of this.
    quiet simple, they are not objects

  22. #22
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: global variable

    Quote Originally Posted by eranga262154
    What you mean of this.
    int x = 3;

    x is a primative;

    Integer x2 = new Integer(2);

    x2 is an object.


    "Because of the collections, yes. And some cases where you needed a nullable value."

    Can you explain it little more.
    Collections need a reference... In other words, you need an object.

  23. #23
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: global variable

    That issue is moot because of the autoboxing to Integer or what ever object it needs to be.

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