943,916 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 15890
  • Java RSS
Jun 16th, 2007
0

public vs private

Expand Post »
Hi i just have a question,
i was doing the pass midterm the question is asking that
" Write a class Jacket, which has a boolean instance variable isReversible and a static int variable numJackets. There are no methods "

correct Ans:
public class Jacket {
private boolean isReversible;
private static int numJackets;
}
------------------------
i just wondering for the last one
can i write like :

public static int numJackets;

is that okay ?


Thank you
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chihwei is offline Offline
5 posts
since Jun 2007
Jun 16th, 2007
0

Re: public vs private

Yes u can ,
U have to understand the deferents between public and private .
If u made it private this means u can only access from inside the class .But using it as public means u can also access this variable from outside the class .This is a simple explanation .
Reputation Points: 10
Solved Threads: 4
Newbie Poster
msi_333 is offline Offline
22 posts
since Jun 2007
Jun 17th, 2007
0

Re: public vs private

that's just the same thing haha..
Reputation Points: 21
Solved Threads: 7
Junior Poster in Training
ProgrammersTalk is offline Offline
83 posts
since Jun 2007
Jun 18th, 2007
0

Re: public vs private

that's just the same thing haha..
Please read the forums rules before posting futher more specialy Keep It Pleasant section
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Jun 18th, 2007
0

Re: public vs private

The whole point of declaring something private (which as it was explained means that it is only accessible within the scope of that class) is to prevent the value of the variable changing without supervision. In your case perhaps we would want the number of jackets to lie between 0 and 13 and so when its value is requested to be changed (through a method like setNumberOfJacks(int newNum) then we can control the way numJackets is changed. Of course there are other more relevant reasons for this as well.

Now looking at the solution, as it stands, one would not be able to do anything with the class (unless you inherit) since there are no methods in the class. I'd say both variables should be declared public since the class looks the same as a C++ struct. Though this goes against the purpose of a class - a class is meant to have methods that control its behaviour.

So why did you ask this question anyway? You put public and you got marked down?
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
147 posts
since Aug 2006
Apr 16th, 2010
-4

haw haw haw, chill out guy!

Click to Expand / Collapse  Quote originally posted by peter_budo ...
Please read the forums rules before posting futher more specialy Keep It Pleasant section
i signed up for a new account just to tell you how anal you are, and then laugh. seriously, relax. maybe some day there will be a language just for forums as to put limitations of things people can say, so they will be more valuable/relative.

insult($username,$lineRefNumber,$statementOfFault,$optionalCommentTowardsMother);

only functions like that wont exist in this language, they will remove anything insulting and possibly even argumentative, generally there will be no means to express your emotion. one day this language will exist, and you will be at rest, until then, continue to scan posts and restrict people's freedom of speech manually.
Reputation Points: 4
Solved Threads: 0
Newbie Poster
tempguy999 is offline Offline
1 posts
since Apr 2010
Apr 16th, 2010
1
Re: public vs private
Is is against the forum rules to spam; and any other forum. When you have nothing useful to add or post new information dont post at all.

ProgrammersTalk's post which was quoted in peter_budo's reply was such:
Quote ...
that's just the same thing haha..
What did it contribute to the discussion? How was it helpful for the question that was initially made. It dind't do anything. So posting to a thread to say nothing is against the rules. You may don't know it, but in this forum such as others, there are sometimes people that just make posts that say nothing just for the reason to increase their post counter.

I am not referring to your post, but to the post that had no contribution and resulted in peter_budo's intervantion.

Also your post is a total violation to the rules. And if you start talking about freedom, then when people sign in there are rules to follow. No one is forcing them to sign in and no one is forcing them to stay to this forum. But they have agreed to the rules. So they must follow them.

You critised others without helping anyone. And I am not saying that you are not allowed to express your opinion, I am saying that in this forum, we don't critise others, we help people with their code. I would like to see you do that some time.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Apr 17th, 2010
0
Re: public vs private
@tempguy999 no point to get in discussion with you as you would be in disadvantage here without swear words...
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: Factorial program
Next Thread in Java Forum Timeline: RE: NEED HELP @ Snake Game in JApplet





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC