Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7574483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:26:48+00:00 2026-05-30T16:26:48+00:00

When using C++ one is not allowed to access a private attribute inside a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:26:49+00:00Added an answer on May 30, 2026 at 4:26 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're using WatiN for testing our UI, but one page (which is unfortunately not
I've two TableViews (one SourceList and one Normal TableView). I'm not using ArrayControllers, just
I have a Coda slider on one webpage (hand-crafted, not using the plugin, but
I would like to multi-thread an application, however one library i'm using is not
When using one's own iPhone for development it's easy enough to access any crash
When using one of the various JDBC template methods I am confused on how
I am using one master page.In that I am registering java scripts and checking
I am using one of snook.ca script for simple slideshow. Here it is in
When you move beyond using one instance for your database, what is the best
I am using one UIViewController as shown: @interface RssViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,BlogRssParserDelegate> I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.