This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes a question about static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a question about static" Watch "a question about static" New topic
Author

a question about static

johny doe
Ranch Hand

Joined: Dec 07, 2007
Posts: 78
the only thing i know about static is that if we
make a method outsite in another class
we can accses this method without making constructor

my question is
when i define a staic variable and a normal variable
a static method and a non static method

what type of variable each type of method can see?
or what about accessing static methods
instead of variables as in the previos question
what accses do we have??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
Static members:-
  • Are loaded into memory when the particular class is loaded.
  • Stay put in memory (hence the word static), although a GC can rearrange all the memory.
  • Go with the class, so should be called by MyClass.itsStaticMember
  • A class has one copy of each static member
  • Each instance of the class has one copy of each non-static (better called instance) field
  • Access to static members is controlled just as for instance members: public private [default] or protected
  • Every instance of a class has access to all its private static members, but remember there is only ever one copy of each, so they all access the same memory location
  • Public static members can be imported into other classes with import static, then used as if they were static members of both classes
  • Static members are in existence and accessible before any instances have been created
  • Because the static members are in memory before the instances are created, the static members cannot find out the memory locations of the instances, so static methods cannot access "non-static" (instance) members.I hope this all helps.

    CR

    [edit]Minor formatting corrections[/edit]
    [ December 30, 2007: Message edited by: Campbell Ritchie ]
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Originally posted by johny doe:
    the only thing i know about static is that if we
    make a method outsite in another class
    we can accses this method without making constructor



    It should be "without making an instance". You don't make any constructor rather you define your constructor to make up your instance.


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    This link will help you further.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: a question about static
     
    Similar Threads
    Abstract class with non-static variables
    Concurrent access on servlet instance variables...
    Reference Variable
    Polymorphic Madness!
    Serializable