Jump to content

two question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
14 replies to this topic

#1
eman ahmed

eman ahmed

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
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
;


#2
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
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.

#3
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
...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'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
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
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
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
So, what does Static actually do? I still don't get it...

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
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
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
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
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?

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
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
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
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
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.

#10
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
...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
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
Yes. The example posted by Sinipull accesses a static variable (count) from a non-static method (the constructor of Dog).

#12
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Yes, that's what's happening in the example. "count++" .
// double answer :P