Hi, I have difficulties to get variable from other class. See class Mod01 and Mod02 below, in class Mod02 I will get ct1=0, but I want ct1=250. Any help will be gratefully. Thank you.

---------------------------------------

final class Mod01 extends FullScreen {   
   int ct,coba;
   String hits;
   
   [B]public int test() { return coba; } [/B]  
   
   Mod02 DisplayMod02;
         
   public Mod01() {}
      
   public void paint(Graphics g) {
      g.setBackgroundColor(0x0);
      g.clear();

      try {Thread.currentThread().sleep(50);} // delay 50 seconds
           catch(Exception e) {}
      
      if (ct==0) {
         ct=1; 
         [B]coba=250;[/B]
         } 
      
      hits="ct="+Integer.toString(ct)+"  coba="+Integer.toString(coba);
      g.setColor(0xefed42);     
      g.drawText("Modul 01 : "+hits,10,10); 
           
      invalidate();
      } 
      
   protected boolean navigationClick(int status,int time) {
      DisplayMod02 = new Mod02();
      getUiEngine().pushScreen(DisplayMod02); 
      return true;
      }
      
   public boolean keyDown(int keycode, int time) {
      if (keycode==655360) close();
         return true;   
      }      
      
   }

-------------------------------------------
final class Mod02 extends FullScreen {   
   int ct,ct1;
   String hits;
   
   public Mod02() {}
      
   public void paint(Graphics g) {
      g.setBackgroundColor(0x0);
      g.clear();

      try {Thread.currentThread().sleep(50);} // delay 50 seconds
           catch(Exception e) {}
      
      if (ct==0) {
         ct=1;
         [B]Mod01 Kuya = new Mod01();  
         ct1=Kuya.test();[/B]
         } 
         
      hits="ct="+Integer.toString(ct)+"  ct1="+Integer.toString(ct1);
      g.setColor(0xefed42);     
      g.drawText("Modul 02 : "+hits,10,10);       
      invalidate();
      } 
      
   public boolean keyDown(int keycode, int time) {
      if (keycode==655360) close();
         return true;   
      }      
    
   }

----------------------------------------

NoCodeTagsError at line 0, redo from start

you can get public variables from any class by initializing setters and getters ... i didn't look at your code

public void setX(){

}

public int (or what ever) getX(){

return x;

}

Sorry to ask again. I'm kind a new here. Where should I put 'public void setX() {}' ? And is there anything inside that public? Thanks.

Hi Can you please explain what are you looking exactly for ?
Your question is
"See class Mod01 and Mod02 below, in class Mod02 I will get ct1=0, but I want ct1=250."
is lil confusing

Are you looking for some constant whose value in Mod01 will be 0 and whose value in Mod02 will be 250 ?

NO problem bro, firstly you have to initialize your variables as public or private according to your usage and give them an initial value for example public static int coba = 0; or to initialize it in the constructor .. as your code like this will return an error bec. how it can return coba without initializing it (or the java automatically initialized it to zero the default value), then your code can work as it is but in the mod1 you can make

public static int get_coba(){

return coba;

}//end of get coba (the same like test)

secondly you call mod01 kuya which will call it's constructor the first thing (which is empty ) so coba wasn't initialized properly . so you must call paint in the constructor or initialize it in another function and simply call it back in the constructor instead of putting paint

I got this message 'C:\Workspace\Passing\Mod01.java:20: non-static variable coba cannot be referenced from a static context' It's happen when I call 'public static int get_coba()' in Mod02 class.

NO you can't make in mod02 you must make it in mod01 and then call it from mod02
mod01 kuya = new mod01();
int ct1 = kuya.get_coba();

that's All..please feel free to ask any time...try it and tell what happen

I have to use ' public int get_coba()' without 'static' then I will get no error. Please see my first program. In 'Mod01' I set 'coba=250'. But when I call 'get_coba' in 'Mod02' I get 0 not 250. How can I get 250 ?

final class Mod01 extends FullScreen {   

   public static int ct;
   public static int coba; 
   public static String hits = "";

   // public Mod01() {} useless

   public int test() { return coba; }   

   Mod02 DisplayMod02;

   public void paint(Graphics g) {
      g.setBackgroundColor(0x0);
      g.clear();

      try {Thread.currentThread().sleep(50);} // delay 50 seconds
           catch(Exception e) {}

      if (ct==0) {
         ct=1; 
         coba=250;
         } 

      hits="ct="+Integer.toString(ct)+"  coba="+Integer.toString(coba);
      g.setColor(0xefed42);     
      g.drawText("Modul 01 : "+hits,10,10); 

      invalidate();
      } 

   protected boolean navigationClick(int status,int time) {
      DisplayMod02 = new Mod02();
      getUiEngine().pushScreen(DisplayMod02); 
      return true;
      }

   public boolean keyDown(int keycode, int time) {
      if (keycode==655360) close();
         return true;   
      }      

   }

-------------------------------------------
final class Mod02 extends FullScreen {   

   public static int ct;
   public static int coba; 
   public static String hits = "";   

   //public Mod02() {} useless also

   public void paint(Graphics g) {
      g.setBackgroundColor(0x0);
      g.clear();

      try {Thread.currentThread().sleep(50);} // delay 50 seconds
           catch(Exception e) {}

      if (ct==0) {
         ct=1;
         Mod01 Kuya = new Mod01();
         kuya.paint();
         ct1=Kuya.test();
         } 

      hits="ct="+Integer.toString(ct)+"  ct1="+Integer.toString(ct1);
      g.setColor(0xefed42);     
      g.drawText("Modul 02 : "+hits,10,10);       
      invalidate();
      } 

   public boolean keyDown(int keycode, int time) {
      if (keycode==655360) close();
         return true;   
      }      

   }

try it

as i said before you have to initialize the coba variable in the first class then you can call it....tell me please how can you call a variable without initializing it, there is a method paint which initialize it to 250 but this method wasn't called so it act as if it wasn't initialized

if you couldn't get it till now send to me your problem statement because i couldn't paly in your code bec. i don't know what the problem statment

Thank you for Abdel_eid. Problem is SOLVED now.

The solution is :

I should write : public static int coba; // This statement is the real solution !

NOT only : int coba;

'kuya.paint();' is not necessary, only give an error message.

When I call 'ct1=Kuya.test();' in Mod02 class, the result is ct1=250.

Even I can do the other way :

public static int coba;

// public int test() { return coba; } // ignore it for this time

In Mod02 class I can just call 'ct1=Kuya.coba;' , and the result is ct1=250 !

I tried both ways in real Handphone and it's working!

Thank you :-)

you are totally right bro. but try to work with the first solution because as long as you are working with large project you will have to make your variable private for security issues so get used to work with setters and getters that's is more better than initializing data as public and calling it from any other class

YOU WELCOME =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.