Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simple (?) problem with different classes

Posted on 2004-04-27
6
Medium Priority
?
186 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 100 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses

916 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