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
Solved

Simple (?) problem with different classes

Posted on 2004-04-27
6
177 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
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

809 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