User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 374,023 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,789 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1101 | Replies: 4
Reply
Join Date: Jun 2007
Posts: 5
Reputation: chihwei is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
chihwei chihwei is offline Offline
Newbie Poster

public vs private

  #1  
Jun 16th, 2007
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Posts: 7
Reputation: msi_333 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
msi_333's Avatar
msi_333 msi_333 is offline Offline
Newbie Poster

Re: public vs private

  #2  
Jun 16th, 2007
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 .
Just walk beside me and be my friend Java problems,Applet Games
Reply With Quote  
Join Date: Jun 2007
Posts: 83
Reputation: ProgrammersTalk is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 7
ProgrammersTalk's Avatar
ProgrammersTalk ProgrammersTalk is offline Offline
Junior Poster in Training

Re: public vs private

  #3  
Jun 17th, 2007
that's just the same thing haha..
The ProgrammersTalk Community | Programming & Marketing | Buying & Selling Script
Hang out place of novice and intermediate programmers
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 1,907
Reputation: peter_budo has a spectacular aura about peter_budo has a spectacular aura about peter_budo has a spectacular aura about 
Rep Power: 9
Solved Threads: 216
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Posting Virtuoso

Re: public vs private

  #4  
Jun 18th, 2007
Originally Posted by ProgrammersTalk View Post
that's just the same thing haha..

Please read the forums rules before posting futher more specialy Keep It Pleasant section
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Join Date: Aug 2006
Location: South Africa, Durban
Posts: 98
Reputation: PoovenM is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
PoovenM PoovenM is offline Offline
Junior Poster in Training

Re: public vs private

  #5  
Jun 18th, 2007
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?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 11:24 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC