Solved

Simple (?) problem with different classes

Posted on 2004-04-27
6
179 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaFX TableView not displaying correctly 3 160
servlet and mdb, jms error 1 81
jmeter usage 4 63
Where to place postgres JDBC driver jar on tomcat 8 70
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses

734 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