• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 43028
76
  • 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.
 
I knew I would regret that burrito. But this tiny ad has never caused regrets:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic