Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

visibility of attributes when class is private?

java attributes class public private visibility concept

  • Please log in to reply
5 replies to this topic

#1 abderrahim

abderrahim

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 58 posts
  • Location:algeria
  • Programming Language:C, Java, PHP, Perl, Pascal, Assembly, Haskell
  • Learning:C, Java, PHP, JavaScript, PL/SQL, Assembly, Haskell

Posted 20 October 2012 - 11:54 PM

Hi everybody here!! I hope you all alright
I'm not good in Java, my problem is with a basic concept: what's the difference of private, public, friendly modifiers when applied to attributes, in the case of a >private class< (I think there is no difference!, we can't read these attributes from outside the class, right?), I hope my question is clear,
Thank's in advance.
  • 0

#2 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 21 October 2012 - 09:52 AM

I don't think there is any need to make a class private in the first place. The only reason for it that I see is nested classes.
  • 0
Speaks fluent Java

#3 nishantenet

nishantenet

    CC Regular

  • Member
  • PipPipPip
  • 38 posts
  • Location:India
  • Programming Language:C, C++, C#, PL/SQL, Visual Basic .NET, Transact-SQL
  • Learning:C, C++, C#, PL/SQL, Visual Basic .NET

Posted 21 October 2012 - 10:01 AM

You can make a class private...(i know its unusual) but you can declare a friend method in it using which you can make a communication to the outer world...
  • 0

#4 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 21 October 2012 - 10:13 AM

You can make a class private...(i know its unusual) but you can declare a friend method in it using which you can make a communication to the outer world...

Yes, but in most scenarios, there is really no effective reason to use private classes, other than for security reasons.
  • 0
Speaks fluent Java

#5 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 21 October 2012 - 10:52 AM

Hi everybody here!! I hope you all alright
I'm not good in Java, my problem is with a basic concept: what's the difference of private, public, friendly modifiers when applied to attributes, in the case of a >private class< (I think there is no difference!, we can't read these attributes from outside the class, right?), I hope my question is clear,
Thank's in advance.


It depends. You can read those attributes if they come from an inner private class regardless of if they're private/public. The class that can read those attributes is the outer class.

Example:

public class OuterClass {
    
    private class InnerClass {
        private int value = 4;
    }
    
    public void printInner() {
        InnerClass inner = new InnerClass();
        System.out.println(inner.value);
    }
    
    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        outer.printInner();
    }
}


This example compiles and executes with an output of
4

  • 1

#6 abderrahim

abderrahim

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 58 posts
  • Location:algeria
  • Programming Language:C, Java, PHP, Perl, Pascal, Assembly, Haskell
  • Learning:C, Java, PHP, JavaScript, PL/SQL, Assembly, Haskell

Posted 21 October 2012 - 12:33 PM

Thank you lethalwire, Chall and nishantenet,
lethalwire, you provide me a clear and an accurate example, thank you!!
  • 0





Also tagged with one or more of these keywords: java, attributes, class, public, private, visibility, concept

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download