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 6058975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:39:34+00:00 2026-05-23T08:39:34+00:00

First of all i created a class called PrivateShirt1 and called a private field

  • 0

First of all i created a class called PrivateShirt1 and called a private field name after creating an object of the class within the same class file, and it worked.

public class PrivateShirt1{
   private String name;

   public static void main(String args[]){               
       PrivateShirt1 s1=new PrivateShirt1();
       s1.name="hi";

       System.out.println(s1.name);    
   }
}

next i created a separate file called PrivateShirt2 in which i put the main method of PrivateShirt1 and performed the function of compiling and calling it. After the modifications the files PrivateShirt1 and PrivateShirt2 look like this:

public class PrivateShirt2{ 

  public static void main(String args[]){               
     PrivateShirt1 s1=new PrivateShirt1();
     s1.name="hi";

     System.out.println(s1.name);    
  }


public class PrivateShirt1 {
   private String name;    
}

and when i compiled the PrivateShirt2 file, it gave an error that the attribute i was trying to call is private.

but then why did this not happen in the previous example? I mean, objects were created in both the cases and hence the rules should be equal for both, right? so why then this partial treatment? could anyone elaborate?

  • 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-23T08:39:34+00:00Added an answer on May 23, 2026 at 8:39 am

    You need to look at java access control. The name variable in PrivateShirt1 has private access. Meaning that field can only be referenced from inside that class. PrivateShirt2 does not have access to that field. The reason your first call worked is because it was called within PrivateShirt1

    http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

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

Sidebar

Related Questions

I created a domain class called Money, with 3 fields. I did a generate-all.
I have created a class called aptSchedule which, like its name suggests, keeps track
first of all, a preface: I'm writing a java class that creates temporary tables
first of all I'm using MySql. I have to create a advance search field
I've created an array of elements called $images (all the elements in the hidden
I have a class which get all the bytes from a file, then it
im making a class to easy manage SQL commands. I created all CRUD and
In my game I created a base class called Entity which I store in
I've created an OpenStruct -like class called Ribbon : class Ribbon < BasicObject def
I have a Java class called CreateThread which creates one thread, everytime an object

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.