The Artima Developer Community
Sponsored Link

Java Answers Forum
static methods

3 replies on 1 page. Most recent reply: Jan 31, 2006 6:11 AM by Madhan Kumar

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Madhan Kumar

Posts: 10
Nickname: madhan
Registered: Mar, 2002

static methods Posted: Jan 30, 2006 4:21 PM
Reply to this message Reply
Advertisement
Hi Guys,
Can anyone explain the Below static methods for me.
My program is:

public class Static2 {
 
 
    private String _mood = _HAPPY;
 
  
  private final static String _HAPPY   = "happy";
   
 private final static String _ANNOYED = "annoyed";
 
   private final static String _ANGRY   = "angry";
 
 
   private static int _instantiations;
 
  
  public static void main( String [] args ) {
   
     
Static2 obj1 = new Static2();
 
 
Static2 obj2 = new Static2();
 
obj1.printMood();
obj2.printMood();
 
obj1.receiveHug();
obj2.receivePinch();
 
obj1.printMood();
obj2.printMood();
 
    }
    
    
    public Static2() {
        _instantiations++;
    }
 
    public static int instances() {	
        return _instantiations;
    }
 
    public void printMood() {
        System.out.println( "I am " + _mood );
    }
 
    public void receivePinch() {
        if( _mood.equals( _HAPPY ) ) {
            _mood = _ANNOYED;
        } else {
            _mood = _ANGRY;
        }
    }
 
    public void receiveHug() {
        if( _mood.equals( _ANGRY ) ) {
            _mood = _ANNOYED;
	} else {
            _mood = _HAPPY;
        }
    }
}


How this _mood is working ? We didn't decalre any variable like _mood? i don't get the Static methods?Please help me.

Thanks in advance.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: static methods Posted: Jan 31, 2006 12:28 AM
Reply to this message Reply
1. You DID declare _mood. It is a class variable of the class Static2.

2. You only have ONE static method (not counting the main method): public static int instances()
Everytime you create an instance of Static2, the variable _instantiations is increased. instances() returns its value.

Static methods can be accessed without needing an instance of the class.

Static variables can also be accessed without a instance.
They also have the same value for all instances.

example:
.
.
.
System.out.println(Static2.instances()); //output will be "0".
Static2 s1 = new Static2(); //increases the counter
System.out.println(Static2.instances()); //output will be "1"
System.out.println(s1.instances()); //output will be "1"
Static2 s2 = new Static2(); //increases the counter
System.out.println(Static2.instances()); //output will be "2"
System.out.println(s2.instances()); //output will be "2"
System.out.println(s2.instances()); //output will be "2"

Note that the static variable _instantiations is the same for all instances.

The NON static variable _mood is NOT the same for all instances as you will see executing your example.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: static methods Posted: Jan 31, 2006 12:32 AM
Reply to this message Reply
Made a mistake:
At the end it should output the counter of s1 and s2 instead of s2 twice.

Madhan Kumar

Posts: 10
Nickname: madhan
Registered: Mar, 2002

Re: static methods Posted: Jan 31, 2006 6:11 AM
Reply to this message Reply
Hi Matthias,

I got understand the concept of static method and static variable well by you.Thanks a lot.

Have a great day.

Flat View: This topic has 3 replies on 1 page
Topic: JNI Calling Signature Problem. Please Help me out Previous Topic   Next Topic Topic: Error: cannot resolve symbol

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use