Program with multiple classes

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Program with multiple classes

Postby corona » Mon Sep 24, 2007 1:13 pm

I have a big lejos program which spanns multiple classes
Code: Select all
package testing;

public class Class2 {
   private int i;
   public Class2(int i) {
      this.i = i;
   }
   public int getI() {
      return i;
   }
}

The above class is used by this class
Code: Select all
package testing;

import lejos.nxt.LCD;
import lejos.nxt.Button;

public class Class1 {

   public static void main(String args[]) {
      Class2 c2 = new Class2(42);
      int i = c2.getI();
      LCD.clear();
      while(!Button.ESCAPE.isPressed()) {
         LCD.drawInt(i, 4, 2);
         LCD.refresh();
      }
   }
}

Now, if I nxjc compile Class1 I get the following:

Class1.java:9: cannot find symbol
symbol : class Class2
location: class testing.Class1
Class2 c2 = new Class2(42);

I have tried nxjc Class2.java first, which seems fine, but compilation of Class1 cannot complete.
I have also tried nxj Class2 but that doesn't work since Class2 doesn't have a main method. I would really not like to hack it with an empty main().

How do I compile my program so Class1 knows Class2?

Thanks
corona
New User
 
Posts: 9
Joined: Wed Sep 05, 2007 9:12 am

Only solution???

Postby corona » Mon Sep 24, 2007 1:27 pm

Is making an inner class like this the only solution?
Code: Select all
package testing;

import lejos.nxt.LCD;
import lejos.nxt.Button;

public class Class1 {
   class Class2 {
      private int i;
      public Class2(int i) {
         this.i = i;
      }
      public int getI() {
         return i;
      }
   }
   
   public static void main(String args[]) {
      Class1 c1 = new Class1();
      Class2 c2 = c1.new Class2(42);
      int i = c2.getI();
      LCD.clear();
      while(!Button.ESCAPE.isPressed()) {
         LCD.drawInt(i, 3, 4);
         LCD.refresh();
      }
   }
}
corona
New User
 
Posts: 9
Joined: Wed Sep 05, 2007 9:12 am

Remove Package sentence.

Postby esmetaman » Mon Sep 24, 2007 7:55 pm

My alternative solution is the following:

Code: Select all
import lejos.nxt.LCD;
import lejos.nxt.Button;

public class Class1 {

   public static void main(String args[]) {
      Class2 c2 = new Class2(42);
      int i = c2.getI();
      LCD.clear();
      while(!Button.ESCAPE.isPressed()) {
         LCD.drawInt(i, 4, 2);
         LCD.refresh();
      }
   }
}



Code: Select all
public class Class2 {
   private int i;
   public Class2(int i) {
      this.i = i;
   }
   public int getI() {
      return i;
   }
}
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 311
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby corona » Tue Sep 25, 2007 8:48 am

Thanks for your reply

However, I found out that using nxjc *.java does the trick, without inner classes
corona
New User
 
Posts: 9
Joined: Wed Sep 05, 2007 9:12 am

Re:

Postby esmetaman » Tue Sep 25, 2007 8:26 pm

HI,

My code example tried to show two separate classes. When you try to compile use the command nxjc Class1.java because that class includes main method.
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 311
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby bbagnall » Sat Oct 20, 2007 5:28 pm

Compile class 2 before compiling class 1 and it will work.
User avatar
bbagnall
Site Admin
 
Posts: 392
Joined: Fri Aug 04, 2006 4:03 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff