|
-
Sep 26th, 2006, 10:26 PM
#1
Thread Starter
Lively Member
global variable
in Java, how do we declare a global variable? All classes can use this variable.
Thanks.
-
Sep 26th, 2006, 10:30 PM
#2
Re: global variable
Isnt that bad programming? I thought classes were to be independant of each other..?
-
Sep 26th, 2006, 10:32 PM
#3
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
-
Sep 27th, 2006, 12:45 AM
#4
PowerPoster
Re: global variable
 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
-
Sep 27th, 2006, 08:10 AM
#5
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
-
Sep 27th, 2006, 10:21 PM
#6
PowerPoster
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Oct 1st, 2006, 12:09 PM
#7
Frenzied Member
Re: global variable
 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
-
Oct 10th, 2006, 09:29 AM
#8
Lively Member
Re: global variable
 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.
-
Oct 10th, 2006, 10:34 AM
#9
Re: global variable
 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
-
Oct 10th, 2006, 02:29 PM
#10
Frenzied Member
Re: global variable
 Originally Posted by vagabon
That would be incorrect due to the fact that there are primative types, look it up.
What???? 
*You're wrong*
-
Oct 10th, 2006, 03:20 PM
#11
Re: global variable
 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
-
Oct 11th, 2006, 09:22 AM
#12
Lively Member
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.
-
Oct 11th, 2006, 02:37 PM
#13
Frenzied Member
Re: global variable
I would have thought just the opposite I know some people that would prefer there be no primatives, but me likes.
-
Oct 11th, 2006, 02:37 PM
#14
Frenzied Member
Re: global variable
By the way, I guess my assumption that you were wrong is wrong
-
Oct 11th, 2006, 02:53 PM
#15
Lively Member
Re: global variable
 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.
-
Oct 11th, 2006, 03:43 PM
#16
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.
-
Oct 11th, 2006, 05:43 PM
#17
Frenzied Member
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.
-
Oct 11th, 2006, 06:15 PM
#18
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.
-
Oct 12th, 2006, 12:04 AM
#19
PowerPoster
Re: global variable
 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
-
Oct 12th, 2006, 12:09 AM
#20
PowerPoster
Re: global variable
 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
-
Oct 12th, 2006, 09:12 AM
#21
Lively Member
Re: global variable
 Originally Posted by eranga262154
What you mean of this.
quiet simple, they are not objects
-
Oct 12th, 2006, 10:05 AM
#22
Frenzied Member
Re: global variable
 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.
-
Oct 12th, 2006, 12:01 PM
#23
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|