This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Two classes in one file  RSS feed

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • 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.
 
Benny Shelac
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!