Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

global variable change thru classes

Thread view: 
Carramba - 09 Apr 2005 10:56 GMT
hi!

I have program with 2 public classes and main.
I have decraded it as 'public int count =0;'
know I want to change value of it in class I call counter (with I call  
from main), then pass it back to another class, who prints output.
main have loop with goes through aray list and prints output.
but I can't get correct value of count becouse it have to change  
dependingly on with char is in array e.i. eather increase or decrease..
how to do it..
I hoppe you understand what I meana...

Signature

thanx in advance

______________________________

John McGrath - 09 Apr 2005 21:18 GMT
> I have program with 2 public classes and main.
> I have decraded it as 'public int count =0;'
[quoted text clipped - 4 lines]
> with char is in array e.i. eather increase or decrease..  how to do it..
> I hoppe you understand what I meana...

Sorry, no.  If you post some code, it will probably be a lot more clear.

Signature

Regards,

John McGrath

iamfractal@hotmail.com - 10 Apr 2005 12:55 GMT
> hi!
>
[quoted text clipped - 7 lines]
> how to do it..
> I hoppe you understand what I meana...

(Deep breath.)

Hi, Carramba!

I'm guessing you have two classes something like these (ridiculous
example, I know, but it covers the points):

public class Main {
  public int count = 0;
  public static void main(String[] args) {
     Counter counter = new Counter();
     counter.ChangeThis(count);
     for (int i = 0, n = count; i < n; i++) {
        System.out.println("Args " + i + " = " + args[i]);
     }
  }
}

public class Counter {
  public void changeThis(int count) {
     count = count + 5;
  }
}

This will not work because Java method parameters are pass-by-value,
and no modification of the count variable in the Counter class will
affect the value of the count variable in the Main class.

Here's the solution:

public class Main {
  public int count = 0;
  public static void main(String[] args) {
     Counter counter = new Counter();
     count = counter.getChangedValue(count);
     for (int i = 0, n = count; i < n; i++) {
        System.out.println("Args " + i + " = " + args[i]);
     }
  }
}

public class Counter {
  public int getChangedValue(int count) {
     count = count + 5;
     return count;
  }
}

.ed

www.EdmundKirwan.com - Home of The Fractal Class Composition


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.