user2157850
user2157850

Reputation:

Implementing Static Methods In A Class

From a book I'm going through:

"Design a class name MyInteger. The class contains:

...blah, blah, blah...

Here's what I've got so far. The top is easy to implement with object.isEven()...

The second, I assume this is just to display results without actually setting the value and changing the object? So I could just do object.isEven(2)?

The last one... that's throwing me off a lot. I have no idea. =/ Please help me out. Thanks in advance.

To clarify:

1.

public boolean isEven(){
     // code
}

MyInteger object = new MyIntger(50);
object.isEven();

2.

public boolean isEven(int num){
    // code
}

MyInteger.isEven(50)???

3.

public boolean isEven(int MyInteger)???

???

Upvotes: 0

Views: 2002

Answers (5)

Paul Samsotha
Paul Samsotha

Reputation: 208994

This seems to be the one that's confusing

boolean odd2 = MyInteger.isOdd(new MyInteger(5));  // static call

You use use an instance of MyInteger to pass as an argument. Another way to pass MyInteger as an argument is:

MyInteger num = new MyInteger(5);
boolean odd2 = MyInteger.isOdd(num);  // static call 

class MyInteger{
    int num;

    public MyIntger(int num){
        this.num = num;
    }

    // Method 1
    public static boolean isOdd(int num){
        ...
    }

    // Method 2
    public boolean isOdd(){
        ...
    }

    // Method 3
    public static boolean isOdd(MyInteger num){
        ...
    }
}

public class TestMyInteger{
    public static void main(String[] args){

        // Method 1 call
        boolean odd1 = MyIntger.isOdd(5);    // static call

        // Method 3 call
        boolean odd2 = MyInteger.isOdd(new MyInteger(5));  // static call

        // Method 2 call
        MyIntger num = new MyIntger(5);  // create instance
        boolean odd3 = num.isOdd();   // instance call

        System.out.println(odd1);
        System.out.println(odd2);
        System.out.println(odd3);

    }
}

Upvotes: 0

Zach Beavon-Collin
Zach Beavon-Collin

Reputation: 245

You would be performing actions upon the MyInteger object rather than just a straight int.

Let's say your private variables and constructor look like this (we don't know exactly because it isn't posted):

private int myInt;

public MyInteger(int thisInt) {
    myInt = thisInt;
}

You will need to implement an accessor method that returns the value of myInt within an instance of the MyInteger class and then use this accessor method in your static method to perform the operation.

So as an accessor method:

public int getInt()
{
    return myInt;
}

And then your static method would reference this method in the same way you would in another program. Note that you have to specify the use of the MyInteger object even within the class:

public static boolean isEven(MyInteger myInteger)
{
    //Code here
}

In terms of calling the static method, it would look something like this:

MyInteger myInteger = new MyInteger(50);
MyInteger.isEven(myInteger);

Here, you are referencing an instance of the MyInteger object (myInteger) rather than the primitive int, but because isEven isn't directly connected to a specific object, you have to tell your code where to find the isEven() method, the MyInteger class.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

Consider this as a pointer, and then you might want to look at this question.

public class MyInteger {
  private int value;

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

  public static boolean isPrime(int value) {
    // I would increment counter then test if the result of value modulo counter 
    // (that is if value % counter != 0) until counter >= square_root(value). 
    // Then the value is prime, otherwise 
    return false;
  }

  public static boolean isEven(int value) {
    return (value & 1) == 0;
  }

  public static boolean isEven(MyInteger m) {
    return isEven(m.value);
  }

  public static boolean isPrime(MyInteger m) {
    return isPrime(m.value);
  }

  public static boolean isOdd(int value) {
    return !isEven(value);
  }

  public static boolean isOdd(MyInteger m) {
    return isOdd(m.value);
  }

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

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

  public boolean isPrime() {
    return isPrime(value);
  }

  public int getValue() {
    return value;
  }

  public void setValue(int value) {
    this.value = value;
  }
}

Upvotes: 0

Sulav Timsina
Sulav Timsina

Reputation: 843

    class MyInteger {
int number;

// CONSTRUCTOR
public MyInteger(int a) {
    number = a;
}

public int getNumber() {
    return number;
}

static boolean isEven(MyInteger myint) {
    if (myint.getNumber() % 2 == 0)
        return true;
    else
        return false;
}
    }

Now the main class:

    public class MainClass {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    MyInteger myInteger=new MyInteger(10);
    boolean result=MyInteger.isEven(myInteger);
    if(result==true)
        System.out.println("true result");
    else
        System.out.println("false result");
        }

    }

Upvotes: 1

Jeff Lee
Jeff Lee

Reputation: 781

For second one, the method is belong to the class. But not the created object. If Your code like this :

MyInteger myInteger = new MyInteger(100);

You can call the method by this

MyInteger.isEven(50);

or

myInteger.isEven(50);

It is not related to 100 which set in object.

Upvotes: 0

Related Questions