Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help me understand when to use static on functions/methods

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me understand when to use static on functions/methods

    My teacher is trying to explain this but it really makes no sense the way he is explaining it. If I understand from my own research only, static attached to a method means that it can be called without referencing it with an object. IE: (this code isn't correct just want to do something general)

    Main function {
          int i = area();    // This one would work
          int t = length();  // This wouldn't work unless accessed by an object?
          Object object = new Object;
     
          object.area(); // Wouldn't work?
          object.length()   // Only way the length function could be run?
     
    }
     
    static int area function {
         blah blah
         return k;
    }
     
    int length function {
        blah blah;
        return k;
    }

    Hopefully this makes sense. Basically I want to know what the static keyword does. And sorry if they aren't supposed to be called functions, I mean methods. I came from C, and this class is an introduction to object oriented design so its still new to me. Any good sites to learn object oriented design for Java from because my teacher is not the greatest? Thank you!


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Help me understand when to use static on functions/methods


  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Help me understand when to use static on functions/methods

    You're about right. Marking something with 'static' allows it to be accessed any code that has access to the class (usually obtained in code by naming the Class itself - such as "System.out"). The best (most authoritative) place to learn Java on your own is from the Sun (now Oracle) docs. The relevant page for 'static':

    Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Always remember what The Jade Warlord says in The Forbidden Kingdom:
    The man who honors his teacher honors himself
    Forbidden Kingdom 8 of 10 - YouTube

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me understand when to use static on functions/methods

    Static in Java means it all related to class , nothing at instance level.

    1) static method or field can be accessed by using Class name and that's the recommended way.
    2) non static field can not be accessed inside static context.

Similar Threads

  1. Hash Functions
    By Blueshark in forum Java Theory & Questions
    Replies: 3
    Last Post: July 2nd, 2012, 03:04 PM
  2. Troubles with toString and static and non-static
    By BadgerWatch in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 3rd, 2011, 05:04 AM
  3. [SOLVED] non static variable this cant be referenced from a static context
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 06:13 PM
  4. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 07:51 PM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM