hi all
I have two question
1-what's the mean of (keyword "static") in any method
2- what' s the diference between the following
public class test{
int value;
public int value;
private int value;
static int value;
two question
Started by eman ahmed, Sep 22 2010 11:23 PM
14 replies to this topic
#1
Posted 22 September 2010 - 11:23 PM
|
|
|
#2
Posted 22 September 2010 - 11:47 PM
Any static declaration inside a class means that that declaration is global and visible to all objects of that class (or other classes if the declaration is public). If the declaration corresponds to a method, then this method can be called from the class itself (<class name>.<method>(<arguments>) ) instead of an object instance (<object>.<method>(<arguments>) ).
static keyword can be combined with any visibility keyword (<none>, private, protected and public). They define different things.
If no visibility keyword is specified, the value is visible to objects of all classes of the same package.
If private is specified, the value is only visible to objects of the class where it's defined.
If protected is specified, the value is visible to objects of the class where it's defined and its descendants.
If public is specified, the value is visible to objects of all classes.
static keyword can be combined with any visibility keyword (<none>, private, protected and public). They define different things.
If no visibility keyword is specified, the value is visible to objects of all classes of the same package.
If private is specified, the value is only visible to objects of the class where it's defined.
If protected is specified, the value is visible to objects of the class where it's defined and its descendants.
If public is specified, the value is visible to objects of all classes.
#3
Posted 23 September 2010 - 01:10 AM
...so, why is that useful?
I've never quite understood the relevance of Static either... From what I'd gathered up to now, it was a bit like final, but not really...
I've never quite understood the relevance of Static either... From what I'd gathered up to now, it was a bit like final, but not really...
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)
#4
Posted 23 September 2010 - 01:30 AM
No, final means that the value defined is constant and cannot be redefined or modified later (only allows one assignment). A variable can be static but not final, or final but not static. Any combination is valid. static and final are different things.
#5
Posted 23 September 2010 - 01:36 AM
So, what does Static actually do? I still don't get it...
From what I can gather, this means that you don't need an instance of your class for your class to refer to that method. But don't you need an instance of your class to run anything in the first place?
And also, what are the implications of making a method or a variable static? In what situation would this be useful?
Sorry for being totally clueless in this... It's something I've wondered about for quite a while.
dbug said:
Any static declaration inside a class means that that declaration is global and visible to all objects of that class (or other classes if the declaration is public). If the declaration corresponds to a method, then this method can be called from the class itself (<class name>.<method>(<arguments>) ) instead of an object instance (<object>.<method>(<arguments>) ).
From what I can gather, this means that you don't need an instance of your class for your class to refer to that method. But don't you need an instance of your class to run anything in the first place?
And also, what are the implications of making a method or a variable static? In what situation would this be useful?
Sorry for being totally clueless in this... It's something I've wondered about for quite a while.
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)
#6
Posted 23 September 2010 - 01:46 AM
Fae. Static is shared content. Static is initialized only once, and all the instances can use the same static field. Take a look at the example:
public class Dog(){
private static int count = 0;
public Dog(){
count++;
}
public static void info(){
System.out.println("There are currently"+count+" Dog objects in your program.");
}
}
public class Demo{
public static void main(String[] args){
new Dog();
new Dog();
new Dog();
Dog.info();
// prints "There are currently 3 Dog objects in your program."
}
}
#7
Posted 23 September 2010 - 01:48 AM
Ooohhh, so if you have multpiple instances of a class, and there's a static variable in the class, then all of the instances use the same variable as opposed to having instances of that variable of their own to play with.
I see, that makes a great deal of sense. Thanks for the answers, and thanks for your patience ^^
So what DBug was saying was, in order to refer to a static variable in a class, you've gotta refer to it as class.variable or class.method(args[]) as opposed to objectinstance.variable or objectinstance.method(args[]), because there is only one instance of the static variable?
I see, that makes a great deal of sense. Thanks for the answers, and thanks for your patience ^^
So what DBug was saying was, in order to refer to a static variable in a class, you've gotta refer to it as class.variable or class.method(args[]) as opposed to objectinstance.variable or objectinstance.method(args[]), because there is only one instance of the static variable?
Edited by Fae, 23 September 2010 - 01:52 AM.
Asked one more question
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)
#8
Posted 23 September 2010 - 01:55 AM
To be a little more specific, you use the example Sinipull posted this way:
Dog dog1 = new Dog(); Dog dog2 = new Dog(); Dog.info();Notice that info method is called using the Dog class not an instance of it (dog1 or dog2) because info method is static. You can even call Dog.info() before having created any dog. It will print that there are 0 dogs.
#9
Posted 23 September 2010 - 02:03 AM
It is possible to access static content from the instance like dog1.info(); but it's not recommended, as it creates unnecessary complexity in reading your program, because it seems like the info() is a part of the instanced object, not the static content. Modern IDE's will give you a warning message if you try that.
However you cannot access non-static content from a static content. It's perfectly clear that non-static content is a part of a single object, and without reference, it isn't possible to refer to any particular object.
However you cannot access non-static content from a static content. It's perfectly clear that non-static content is a part of a single object, and without reference, it isn't possible to refer to any particular object.
#10
Posted 23 September 2010 - 02:08 AM
...but you can access static content from non-static content, I'd assume?
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)
#11
Posted 23 September 2010 - 02:10 AM
Yes. The example posted by Sinipull accesses a static variable (count) from a non-static method (the constructor of Dog).
#12
Posted 23 September 2010 - 02:10 AM
Yes, that's what's happening in the example. "count++" .
// double answer :P
// double answer :P


Sign In
Create Account


Back to top









