Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class accessing inner class privates?

Class Outer
{
 ...
    private class Node
    {
      private T data;
      ...

      private T getData()
      {
         return data;
      }
    }
}

What's the purpose of using set and get methods if the outer class can access inner class private members? What's the purpose of making inner classes private? Package access?

like image 863
ShrimpCrackers Avatar asked May 09 '10 05:05

ShrimpCrackers


People also ask

Can inner class access private?

Java inner class is defined inside the body of another class. Java inner class can be declared private, public, protected, or with default access whereas an outer class can have only public or default access.

How do I access an inner class from another class?

Accessing the Private Members Write an inner class in it, return the private members from a method within the inner class, say, getValue(), and finally from another class (from which you want to access the private members) call the getValue() method of the inner class.

Can outer class access inner class private methods?

It can access any private instance variable of the outer class. Like any other instance variable, we can have access modifier private, protected, public, and default modifier. Like class, an interface can also be nested and can have access specifiers.

Why can outer Java classes access inner class private members?

Given these requirements, inner classes have full access to their outer class. Since they're basically a member of the outer class, it makes sense that they have access to methods and attributes of the outer class -- including privates.


2 Answers

Private Inner classes are written when you do not want the class to be exposed to external classes within or outside the package. They are used only inside the outer level class.

The getters and setters generally do not make sense inside a private classes because you can access the instance variables anyways.

like image 69
Snehal Avatar answered Oct 22 '22 23:10

Snehal


You could skip trivial getters and setters, but often those methods are non-trivial (a common case is the 'lazy load' pattern).

Edited to add: Lazy load is when you only instantiate a member when the data is requested. It's used when getting the data is not always needed and is expensive to get.

class a
{
private:
    int m_aNumber;
    bigDeal *m_pBig;

public:
    a() { m_aNumber = 0; m_pBig = NULL; }
    ~a() { if (m_pBig) delete m_pBig; }

    // trivial
    int get_aNumber() { return m_aNumber;}
    void set_aNumber(int val) { m_aNumber = val; }

    // lazy load
    bigDeal *get_Big()
    {
        if (m_pBig == NULL)
        {
            // bigDeal::bigDeal() downloads data from Mars Rover
            // takes 20 minutes, costs $1.2 million dollars to run
            m_pBig = new(bigDeal); 
        }
        return m_pBig;
    }
    void set_Big(bigDeal *val)
    {
        m_pBig = val;
    }
}
like image 34
egrunin Avatar answered Oct 22 '22 21:10

egrunin