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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:54:52+00:00 2026-06-02T15:54:52+00:00

An interviewer asked me this: class Employee{ private string empname; public String getEmpname() {

  • 0

An interviewer asked me this:

class Employee{
    private string empname;

    public String getEmpname() {
        return empname;
    }
    public void setEmpname(String empname) {
        this.empname = empname;
    }
}

class EmpDetails{    
    private static Employee emp;
    public static List fillData(){
        emp=new Employee();
        List l=new ArrayList();
        System.out.println("static after new creation fillData"+System.identityHashCode(emp));
        emp.setEmpname("suresh");
        emp.setDesignation("Sr.Software Engineer");
        l.add(emp);
        emp=new Employee();
        System.identityHashCode(emp);
        System.out.println("static after new creation fillData"+System.identityHashCode(emp));
        emp.setEmpname("Prasad");
        emp.setDesignation("Software Engineer");
        l.add(emp);
        return l;    
    }
}

What happens if define below

private static Employee emp;

What is advantage with define static and non-static non access modifer with employee object?

  • 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-06-02T15:54:55+00:00Added an answer on June 2, 2026 at 3:54 pm

    If a field is defined static, then the value of that field is shared by all the instances of a that particular class. In your case how ever, that field is defined as private, which restricts instances of the class to access it outside the class. When your fill dataget() called it will create the list of Employee and static emp field will hold the value of the last emp(“Prasad”). If any other instance of class EmpDetails is created and you try to access emp without calling fillData, using some other method e.g. GetEmp(), then it will return the last value for emp which was set to “Prasad”.

    With respect to design, this approach is not correct, as EmpDetails class will be pointing to one Employee due to static Employee object.

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

Sidebar

Related Questions

This question was asked at interview. Say I have a contract. [ServiceContract] public interface
I was asked this question in an interview: Is string a reference type or
An interviewer recently asked me this question: given three boolean variables, a, b, and
The interviewer showed me a code like this and asked me whether it would
Today, an interviewer asked me this question. My immediate response was that we could
i was asked this question in my interview that how to make a class
Interviewer asked me about this today ...is there an answer ?
This question was asked in an interview for my friend. The interviewer asked to
I was asked this question in a job interview. The interviewer and I disagreed
An interviewer asked me that How can you implement a class Foo, where you

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.