Solved

Simple (?) problem with different classes

Posted on 2004-04-27
6
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 25 total points
ID: 10932050
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
ID: 10932086
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
ID: 10932114
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:zzynx
ID: 10932387
>> 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
ID: 10932552
>>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
ID: 10936101
All right. Success.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

617 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