Results 1 to 4 of 4
  1. #1
    tyang is offline Member
    Join Date
    Jan 2010
    Posts
    44
    Rep Power
    0

    Default Public, private or (nothing) class

    For a class construction, when should I use pubilc, private or (nothing)?

    Many thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The short answer is: Yes.

    The long answer is: use what is appropriate for the situation. Most of your constructors will be public, but not all, and in fact some will be private (such as enum constructors).

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    It depends on who or what is going to use the class. If everyone is going to use it, then make it public. If only subclasses and packages will use the class, then make it protected. If only the package will use it, use nothing. If only the outer class will use it, use private.



    Here is a brief tutorial on it: Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    EDIT: Fubarable beat me by two minutes. :(
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  4. #4
    tyang is offline Member
    Join Date
    Jan 2010
    Posts
    44
    Rep Power
    0

    Default

    Thanks to both of you!
    Special thanks to Lil_Aziz! Very clear explanations

Similar Threads

  1. Replies: 3
    Last Post: 10-06-2009, 08:14 PM
  2. Replies: 14
    Last Post: 05-28-2009, 05:44 AM
  3. How to access private data types from public classes?
    By kevzspeare in forum New To Java
    Replies: 3
    Last Post: 03-07-2009, 04:19 AM
  4. different multiple public class and main class
    By mr idiot in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 12:10 PM
  5. Public class variable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •