| | |
public vs private
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2007
Posts: 5
Reputation:
Solved Threads: 0
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
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
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 .
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
Programming forums,phpbb3 Styles,
Java Forums,Web Development Forums
Programming forums,phpbb3 Styles,
Java Forums,Web Development Forums
that's just the same thing haha..
The ProgrammersTalk Community | Programming & Marketing | Buying & Selling Script
Hang out place of novice and intermediate programmers
Hang out place of novice and intermediate programmers
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)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Aug 2006
Posts: 137
Reputation:
Solved Threads: 11
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
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
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? ![]() |
Similar Threads
- rsa public key?? (Computer Science)
- PHP, ASP, ColdFusion, what's your fav? (IT Professionals' Lounge)
- accessing private data members (C++)
- program help (C++)
- Classes (C++)
- windows 2003 server boot problem,,,, imdying here... (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: how to make standalone code out of NetBeans IDE
- Next Thread: Java rmi-ps help
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows






