Hello,

For java interface, In one class I have the following code

public class ObjectA
{

    public void SampleA()
    {  
         //do something 1
    }  

    public void SampleB((int aPosition)
   {
       // user enter some value
   }

}

I have another class which is not related to the other class, only interested in the message. I know how to send SampleA message to object reference Piggy. What I don't know is how do I send SampleB message to same object reference Piggy. Message is from a user when he/she enters a value.

public class  ObjectA
{
   // Instance variable
  private Pig Piggy;

  public void ExampleA()
  {
       Piggy.SampleA();  // the SampleA sends a message to the   
                                   //  object reference Piggy

  }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Then you either have to call Piggy.SampleB() or from within the SampleA() method, you have to call SampleB(). Btw, methods should be named in camelcase starting with a lowercase letter: nameMethodsLikeThis() and Objects should be named the same way (although Objects, preferably, should have pretty short names).

Above had typo error should have been exampleA() not ExampleA(), as you have pointed out.

Well thats what I thought. I first tried in the exampleA() method..

piggy.SampleB(); // returned an error message SampleB in Square cannot be applied to (). Square is the interface I created in BlueJ.

I then tried in the same exampleA() method

piggy.SampleB(int aPosition) // error message recieved: class expected.

What else could it be.

If you want to call the SampleA or SampleB methods, you have to do so using an ObjectA. The only alternative is to declare the SampleA method as static: public static void sampleA(){} and then from within a method in a different class, you can say ObjectA.sampleA(); . So to demonstrate your alternatives:

public class Animal{
int age = 0;
int weight = 0;

public static void killAllAnimals(){
System.out.println("BOOM, all animals on earth just died.");
}

public void setAge(int newAge){
age = newAge;
}

}
public class AnimalTester{

public static void main(String[] args){
Animal a = new Animal();
a.setAge(10);
Animal b = new Animal();
b.setAge(20);

Animal.killAllAnimals();

}

}

As you can see, static methods get called by putting Classname.staticMethodName(); whereas non-static methods, which are called instance methods, can be called by creating a new Object, then using the name of the Object to call the method. For more information on when you should use static methods vs. when you should use class (static) methods, see these links:

http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
http://leepoint.net/notes-java/flow/methods/50static-methods.html

P.S. I don't know why my example is so violent today!

Edited 6 Years Ago by BestJewSinceJC: n/a

The unit I am studying does not cover static method nor has it ask to use one. I may have to give you additional code to show you what I have done, which is quite a lot. Could I post the code in attachment?

create an Object of the class type - do you mean like a super class then have an subclass.

No, make your own class, lets call it YourClass. Then to call any methods from YourClass you have to say YourClass obj = new YourClass(); and use the 'obj' to call methods from YourClass.

Actually ignore abovr. What I have been asked is to use local variables string to reference objects, which I am having problems. I have used // to show you where I am having problems. Mainly in the public class Lab, public void methodA(). Thanks in advance...

public interface table
{
public void example();
public void Beep(int abeep);
}


public class SmallAlien extends Alien implements table
{
public void example()
{
System.out.println("Hello");
}


public void sound(int aValue)
{
// checks some value in a conditional for both public void
// example  and public void Beep(int abeep) are here
}
}


public class Lab
{
// Instance variables, table is the interface
private table test1;


public int fromUser()
{
String input;
int Number;
input = Dialog.request("Please enter a value");
Number = Integer.parseInt(inputString);
}


public void methodA()
{
String Response = "Response" + test; // this is wrong


test. example(); // example sends some message to test. No
// problem here it works
// then I want to store the message to Response... so the
// output would show whatever the message it is  will
// reference Reponse. This is my first problem


test.Beep(Number) // Then here I whatever value is from
// fromUser() method the message sound is sent to Response,
// but as you can see this is wrong as  well.
}
}

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.