• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Two classes in one file

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have got two classes in one file in here. By what name can I save this class ? The one which has main() in it ?
If I want to use class valHold in some other class , how can I use it ?Now this value of i is changed to 30 by v.i=30; Then another(v,i) is called. The v and i are passed to another(). At this time value of v.i is 30 and i is 99. i is changed to 0 in another(). v.i is changed to 20. So now v.i has value of 20. A new instance of ValHold is created woth reference as vh. This ValHold object has value of i as 10 again. v is assigned vh. So first ValHold that v referred to is now ready for garbage collection as it does not have any reference pointing to it. Now if we print v.i, it should be 10, i should be 0. When control goes back to amethod() and if we print v.i, it should be 10 again as earlier ValHold object is garbage collected. So I think output of this program should be 10010. When I checked the answer, it says output is 10020. Why is that, can someone please explain me ?

Thanks
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A source file can have only a single public class - so the file must be called ObParm.java.

If a class is important enough to be used in several places then it should be public, and stored in its own source file.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may only have one single public class in a source file. If you make class ValHold public too (within its own source file), it is accessible in any other class. However, you should use getter/setter methods for ValHold instead of manipulating i directly.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic