Solved

Simple (?) problem with different classes

Posted on 2004-04-27
6
174 Views
Last Modified: 2010-03-31
This looks like something I really should know, but Ive never had to deal with it until now...

Say i have 2 classes

class a()
   num=3
   classb.increase
   print num

class b()
   num++

the output will be 3, instead of 4. What am i doing wrong?

Thanks in advance


Full program listing:
***************************
class Networky  {
public int num=1;
      
      public Networky() {      }

      public void gameloop(){
      try{
      GameHandler gh = new GameHandler();
      gh.increase();
      System.out.println(num);
      }catch (Exception e) {}
      }

      public static void main(String args[]) {
            Networky mainFrame = new Networky();
            mainFrame.gameloop();                  
      }
}

class GameHandler extends Thread {

   public GameHandler () { }

   public void increase() {
        Networky net = new Networky();
            net.num++;
   }
}
0
Comment
Question by:hippydaz
  • 4
  • 2
6 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 25 total points
Comment Utility
This prints "2":

class Networky  {
public int num=1;
     
     public Networky() {     }

     public void gameloop(){
     try{
     GameHandler gh = new GameHandler(this);
     gh.increase();
     System.out.println(num);
     }catch (Exception e) {}
     }

     public static void main(String args[]) {
          Networky mainFrame = new Networky();
          mainFrame.gameloop();              
     }
}

class GameHandler extends Thread {

   private Networky nw = null;
   public GameHandler (Networky networky) { nw = networky; }

   public void increase() {
          nw.num++;
   }
}
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
In increase() you just
1) created a *new* Networky instance
2) incremented its variable num
3) throw it away when leaving the function

You did never increment the num variable of the Networky instance called "mainFrame".
0
 

Author Comment

by:hippydaz
Comment Utility
I knew it was getting increased then disappearing, but i had no idea why.

The explanation is slightly more complicated than i was expecting (i though i just wasnt declaring my integers properly or something) but thatll explain it.

Thanks.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> The explanation is slightly more complicated than i was expecting
But you do understand it?

Thanks for accepting.
That keeps us answering your future questions too.
0
 

Author Comment

by:hippydaz
Comment Utility
>>But you do understand it?

More or less... I understand the concepts of how instances work, im just a bit fuzzy on creating instances as a whole. Ill understand the more i use them.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
All right. Success.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now