-1

I have two questions - I'm a beginner in Java but have a huge java project :(

  1. If I implement a class (which is an interface that has two void methods) and I try to write another method in my class, it compiles but at run time the method is skipped? What could I be doing wrong?

  2. How do I run a class from another class that is not the main. So basically the main runs a class which then calls another class...

I know these questions are pretty limited in terms of content but the program is really complexed at its just impossible to explain everything :(

Any help would be greatly Appreciated :) Thanks!!

--- updated - as requested

 class FriendLists implements Results {
     public void processCommunication (Communication d)  {...}

     public void postProcess() {...}

     //the above two run perfectly

     //I don't know what to do next. 
     //I'm trying to create a link to my other class, to use the values values 
     //but when compiled and running it skips this method (and all other methods except the above two

     public void processCommunication(AggregatedDirect f) {
     //does something from the class I'm trying to run
      man = f.getNumTargets(); //getNumTargets is a value in the AggregatedDirect Class
     }

,

interface Results {
void processCommunication (Communication c) throws SAXException;
    void postProcess();

}

,

  public  class AggregatedDirect extends Communication {
ArrayList<Integer> targets;


public AggregatedDirect(Direct d) {
    super();
    targets = new ArrayList<Integer>();
    this.type = d.getType();
    this.invocSerial = d.getInvocSerial();
    this.serial = d.getSerial();
    this.usage = d.getUsage();
    this.messageType = d.getMessageType();
    this.characterID = d.getCharacterID();
    this.characterStatus = d.getCharacterStatus();
    this.locationID = d.getLocationID();
    targets.add(d.getTargetCharacterID());
    this.targetCharacterID = -1;
    this.targetCharacterStatus = -1;
    this.targetCharacterLocationID = -1;
    this.message = d.getMessage();
    this.time = d.getTime();
    this.annotation = d.getAnnotation();
}

public void addTarget(int targetCharacterID) {
    targets.add(targetCharacterID);
}

public void addTarget(Direct d){
    addTarget(d.getTargetCharacterID());
}

public int getNumTargets() {
    if (targets == null)
        return -1;
    else
        return targets.size();
}
public ArrayList<Integer> getTargets() {


     return targets;
    }
}
  • Describe class Communication here.
  • Abstract superclass of all the communication
  • subclasses.
  • in effect - processes an XML file and separates it

    • Direct extends communications // it basically one of the String Values in the XML file
Rabiani
  • 143
  • 2
  • 5
  • 12
  • 1
    Please show us your code, it makes life easier for everyone :-) – Steve Jan 29 '12 at 00:52
  • 1
    Can we see some code in order to understand better about your problem? – Yasin Okumuş Jan 29 '12 at 00:52
  • Also, can you explain what it means the method is skipped? Also, what does it means you run a class? You run methods, not classes. – Luis Jan 29 '12 at 00:54
  • I checked your other question, I want to ask: Is this a desktop project? You have to have a main method which you must start something like an avalache, but it seems that you don't know about it. I doubt if this is a web project something like J2EE kind? – Yasin Okumuş Jan 29 '12 at 00:57
  • Thank you all for your replies :) I've added accordingly :) – Rabiani Jan 29 '12 at 00:59
  • Luis - Sorry, that's what I mean, Run methods from the class sorry. When I call on the method (even just a print statement) the method is just jumped and nothing is printed. Its not a Desktop Project - the project processes a bunch of XML files which contain ChatLogs – Rabiani Jan 29 '12 at 01:01
  • if you are not good in java why are you doing it in java? – Dave Newton Jan 29 '12 at 01:41
  • Majority of the code was written already in Java. I'm doing further analysis – Rabiani Jan 29 '12 at 02:30

2 Answers2

1

You have to call a method to run it, so let's say you have a method called

public void someMethod(){ 
   // method contents
}

in order to run it you'll have to call someMethod(); from within that method's scope.

To call a method from another class you generally have to create an instance or object of that class first. Let's say the class is called OtherClass and there's a method in that class called otherMethod(), in your current class you'll have to create an object of type OtherClass like this:

OtherClass otherClass = new OtherClass();

then call the other method using

otherClass.otherMethod();

If this answer is too simple let us know. :)

rtheunissen
  • 7,347
  • 5
  • 34
  • 65
  • I've tried this but I get a compiling error: logProcessor/FriendLists.java:8: cannot find symbol symbol : constructor AggregatedDirect() location: class logProcessor.AggregatedDirect AggregatedDirect aggregateddirect = new AggregatedDirect(); Any suggestions to why that might be? I'm not good with Java :( – Rabiani Jan 29 '12 at 01:11
1

If you add another method to your class, but that method is not defined in your interface, and you're using the Interface as the type when instantiating the object, then the Java runtime will not be aware that method exists.

For instance:

Class MyClass implements List {

    public void method1() {
        // do stuff
    }

    public void method2() {
        // do other stuff
    }

    public void method3() {  // not defined in Interface
        // do yet other stuff
    }

}


Interface List {

    void method1();

    void method2();

}

Now, if a service class in your code returns an object of type "List", then your calling class won't know the subtype:

 List list = getMyList();

 list.method3();  // doesn't know what this is because it's type List not type MyClass
jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
  • Thanks :) That makes sense. The problem I have is if I add anything to the interface class other classes that have been created need tweaking (I'm talking 93 other classes) Is there any way I could use a method to sort of override it or something?? – Rabiani Jan 29 '12 at 01:15
  • This is where an abstract class could come in handy, assuming that the new methods are going to do the same thing in all 93 other classes. Then you can use a good find/replace tool on the Linux command line to add `extends MyAbstractClass` to the class names of all 93 classes. – jamesmortensen Jan 29 '12 at 02:31