Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the idea behind private attribute access inside main? Java x C++

When using C++ one is not allowed to access a private attribute inside a main function. Example:

#include <iostream>
using namespace std;
class Test {
    private: int a;
    public:
       Test(int value) { a = value; }
       int getValue() { return a; }
};
int main (int argc, char *argv[]) { 
    Test test2(4);
    cout << test2.a; // Compile error! Test::a is private within this context
    cout << test2.getValue(); // OK!
    return 0;
}

It is clear why there is an error when accessing private attributes outside class methods, since C++ do not have main functions inside classes.

However, in Java it is allowed:

public class Test {
    private int a;
    public Test(int value) { a = value; }
    public int getValue() { return a; }
    public static void main (String args[]) { 
        Test test1 = new Test(4);
        System.out.println(test1.a);
    }
}

I understand in this case main is INSIDE the Test class. However, I cannot understand the idea WHY is this allowed, and what is the impact of this in the development/management of the code.

When learning C++, I once heard "Classes shouldn't have a main. Main acts with or uses instances of classes".

Can someone shed some light on this question?

like image 534
Moacir Ponti Avatar asked Feb 29 '12 12:02

Moacir Ponti


2 Answers

You are looking at this from the wrong point of view. The question is not why main can acces the class internals. There is not one 'main' in Java. The important difference to this respect is that for C++ there is a single entry point into the application that is main, while in Java a single application can have multiple entry points, as many as one per class. The entry point must be a static method (member function in C++ jargon) of a class with a particular signature, and the behavior is exactly the same as for other static methods of the same class.

The reason that Java can have multiple entry points is that you tell the VM on startup where (what class) you want to start your application in. That is a feature that is not available in C++ (and many other languages)

like image 131
David Rodríguez - dribeas Avatar answered Nov 01 '22 13:11

David Rodríguez - dribeas


You can actually do the same in C++:

class Test {
    private: int a;
    public:
       Test(int value) { a = value; }
       int getValue() { return a; }
       static void Main()
       {
          Test t(10);
          cout << t.a;
       }
};

It's as simple as that: in both languages, private variables are accessible only from inside the class.

However, I cannot understand the idea WHY is this allowed.

It's just a language feature. If you weren't able to access privates from inside the class, what could you do with them?

Also, not that access-levels are class-wide, not instance-wide. That might be throwing you off. That means you can access a different instance's privates from an instance of the same class. Also, in C++, there's the friend keyword that gives you the same privileges.

like image 28
Luchian Grigore Avatar answered Nov 01 '22 12:11

Luchian Grigore