4 Replies - 764 Views - Last Post: 23 March 2011 - 06:14 PM Rate Topic: -----

#1 Watson18  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 07-March 11

Confusion with static methods

Posted 22 March 2011 - 05:49 PM

Hey Dream.In.Code Community ,

I'm an IT major in college and have been programming for a year now. Right now in my java class I'm having some trouble with static methods. For my current project I have to make a program that includes,


An int data field named value that stores the int value represented by this object.
A constructor that creates a MyInteger object for the specified int value.
A get method that returns the int value.
Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively.
Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively.
Static methods isEven(MyInteger), isOdd(MyInteger), and isPrime(MyInteger) that return true if the specified value is even, odd, or prime, respectively.
Methods equals(int) and equals(MyInteger) that return true if the value in the object is equal to the specified value.
A static method parseInt(char[]) that converts an array of numeric characters to an int value.
A static method parseInt(String) that converts a string into an int value.

I'm just not sure what the difference is between the static method requirement and the regular methods. This is what I have so far,

 

package chap10;
import java.util.Scanner;
public class myInteger {
	public static int value;
	
	public static void main(String[] args) {
		System.out.println("Enter a value that you want to test:     \n");
		Scanner input = new Scanner(System.in);
		value = input.nextInt();
		
	}

	public myInteger(int value) {
		getValue();
		myInteger.value = value;
		
		
	}

	public static int getValue() {
		return value;
	}
	public static boolean isEven() {
		boolean isEven = false;
		return isEven;
	}
	
	public static boolean isOdd() {
		return false;
	}
	public static boolean isPrime() {
		
		return false;
	}
	public boolean equals() {
		
		return false;
	}

	public static int parseInt(int value) {
		return value;
		
	}
	
}




Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Confusion with static methods

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Confusion with static methods

Posted 22 March 2011 - 06:12 PM

The regular methods act on a MyInteger object

Actually your

static int value; is wrong should be simply a int value;

so constructor

class MyInteger {

   private int value;

   public MyInteger(int value) {
     this.value = value;
   }

   // the getter that returns the value
   int getValue() {
     return value;
   }


So now you can do

MyInteger a = new MyInteger(10);
MyInteger b = new MyInteger(20);
now
a.getValue(); will return 10
b.getValue(); will return 20

Now let see you isEven() method

You can effectively have to isEven() method. One that refer the "value" kept in your object MyInteger. So you can have

boolean isEven() {
  return value % 2 == 0;
}


This method will return true if you
a.isEven(); because 10 is even

Now you can also have a static isEven() that will work with an int passed as parameter

static boolean isEven(int x) {
   return x % 2 == 0;
}


now
MyInteger.isEven(15); will return false

note that we write MyInteger.isEven(15); and not a.isEven(15); because the action is not performed on ann actual MyInteger object it is a method that can be invoked even if a MyInteger object has not beeen created.

Actually, the static method can be in any class. We usually group them by functionnality, here it works on an integer value after all.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Confusion with static methods

Posted 22 March 2011 - 06:19 PM

 
import java.util.Scanner;
public class MyInteger {
	
	private int value;
	

	public MyInteger(int value) {
		this.value = value;
	}

	public int getValue() {
		return value;
	}
	public boolean isEven() {
		return value % 2 == 0;
	}

	public boolean equals(MyInteger another) {		
		return value == another.value;
	}

	
	public static void main(String[] args) {
		System.out.println("Enter a value that you want to test:     \n");
		Scanner input = new Scanner(System.in);
		int x = input.nextInt();
		
		MyInteger mi = new MyInteger(x);
		System.out.println("Value entered: " + mi.getValue());
		System.out.println("Is Even: " + mi.isEven());
		
		MyInteger other = new MyInteger(100);
		System.out.println("Is mi equals other: " + mi.equals(other));
	}

}



Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7577
  • View blog
  • Posts: 12,735
  • Joined: 19-March 11

Re: Confusion with static methods

Posted 22 March 2011 - 07:22 PM

It looks like Paul has covered your code pretty thoroughly, so I'll just add a little clarification on the meaning of "static". When you declare an int as a class field (we're not talking about a method variable, that's another story) it's either an instance field or, if you tell it to be, a static field. An instance field pertains to this particular instance of a class, while a static field pertains to the class as a whole. If you had a Car class, you might declare licensePlateNumber as an instance field, since each car has its own license plate, but you'd declare numberOfWheels as a static, since it's going to be shared by all the Cars you create. Okay, you might have a six-wheeled car, but let's not quibble.

Instance methods have access to a particular instance's data - all of it - and no other data unless it's given to them as a message (method call). Static methods only have access to static data, data shared by all members of the class. This is because static members run without reference to a particular instance of the class - you don't need to instantiate Math to get Math.abs(a).

So with this in mind, you can see why a static getValue method doesn't make much sense - a getter is interested in a particular instance, and the class doesn't even know if it has any instances (usually, and you'll know if you write an exception to this). Likewise any method returning information about a particular instance has to be associate with that instance.

On the other hand, maybe you don't want to have to make a MyInteger in order to tell whether 3 is odd or even. Since you've already got the functionality to determine whether a number is odd or even, you can just offer it up as a static. Actually, you might want to write it the other way around - write the static method
public static boolean isEven(int i)
{
  return i%2==0;
} 


and then for the instance version call that:

public  boolean isEven()
{
  return isEven(this.value);
} 



That's called code reuse.

I hope that gives you some ideas about why Paul gave you the advice he did.
Was This Post Helpful? 2
  • +
  • -

#5 Watson18  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 07-March 11

Re: Confusion with static methods

Posted 23 March 2011 - 06:14 PM

Thanks Paul and Jon, really cleared up the subject for me, I guess it's the way my professor explained it that made me confused.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1