944,144 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1918
  • Java RSS
Nov 10th, 2009
0

public void close () VS. private void close ()

Expand Post »
Can someone please explain what both of these mean and the good details about them. Please also explain the parameters of how to use it.
Reputation Points: 17
Solved Threads: 1
Junior Poster in Training
COKEDUDE is offline Offline
82 posts
since Oct 2009
Nov 10th, 2009
0
Re: public void close () VS. private void close ()
Hello.
Adding the private modifier to a method means that you can ONLY call it from within that class.
The public modifier means that you can access that method with the dot (.) operator from other classes.
Java Syntax (Toggle Plain Text)
  1. public class Foo{
  2. public Foo{
  3. doSomethingInternal(); // Legal (This is the same as this.doSomethingInternal();
  4. doSomethingExternal(); // Legal (This is the same as this.doSomethingExternal();
  5. }
  6. public void doSomethingExternal(){/*code*/}
  7. private void doSomethingInternal(){/*code*/}
  8. }
Java Syntax (Toggle Plain Text)
  1. public class Bar{
  2. public static void main(String[] args){
  3. Foo foo = new Foo();
  4. foo.doSomethingExternal(); // Legal
  5. foo.doSomethingInternal(); // NOT Legal
  6. }
  7. }

Hope that clears things up =)
Last edited by llemes4011; Nov 10th, 2009 at 6:29 pm. Reason: correction
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008
Nov 10th, 2009
0
Re: public void close () VS. private void close ()
Good points about the public modifier - You can modify variables in the class through gettor/settor methods. These methods can be called by using the dot (.) operator.
Good points about the private modifier - It allows methods that should ONLY BE CALLED BY THE CLASS (initing vars, handling events, ect) to be non-accessible to anything outside the class. These methods cannot be called by using the dot (.) operator.

(Sorry for double post)
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008

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.
Message:
Previous Thread in Java Forum Timeline: Unable to expand window
Next Thread in Java Forum Timeline: Help with Array





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


Follow us on Twitter


© 2011 DaniWeb® LLC