in Java, how do we declare a global variable? All classes can use this variable.
Thanks.
Printable View
in Java, how do we declare a global variable? All classes can use this variable.
Thanks.
Isnt that bad programming? I thought classes were to be independant of each other..?
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
Quote:
Originally Posted by ComputerJy
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
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)
OK
Thanks
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.Quote:
Originally Posted by |2eM!x
Hope that made sense :D
That would be incorrect due to the fact that there are primative types, look it up.Quote:
Originally Posted by ComputerJy
http://answers.yahoo.com/question/in...5114209AABH0HP :confused:Quote:
Originally Posted by vagabon
What???? :confused:Quote:
Originally Posted by vagabon
*You're wrong*
I know, that's why I looked it up ;)Quote:
Originally Posted by System_Error
EDIT: Actually, he has a good point http://forum.java.sun.com/thread.jsp...71077&tstart=0
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!).
I would have thought just the opposite :sick: I know some people that would prefer there be no primatives, but me likes.
By the way, I guess my assumption that you were wrong is wrong :D
lolQuote:
Originally Posted by System_Error
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.
Since Java5 and autoboxing, it's fine. Before that, primitives were a pain.
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.
Because of the collections, yes. And some cases where you needed a nullable value.
Quote:
Originally Posted by CornedBee
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.
Quote:
Originally Posted by vagabon
What you mean of this.
quiet simple, they are not objectsQuote:
Originally Posted by eranga262154
int x = 3;Quote:
Originally Posted by eranga262154
x is a primative;
Integer x2 = new Integer(2);
x2 is an object.
Collections need a reference... In other words, you need an object.Quote:
"Because of the collections, yes. And some cases where you needed a nullable value."
Can you explain it little more.
That issue is moot because of the autoboxing to Integer or what ever object it needs to be.