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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:06:11+00:00 2026-05-15T16:06:11+00:00

I have a class, class MyClass { private int val; public static final MyClass

  • 0

I have a class,

class MyClass {
    private int val;
    public static final MyClass myObject = new MyClass(1);
    MyClass(int a){
        val = a;
    }
    public int getVal(){
        return val;
    }
    public MyClass func1(){
        MyClass temp = myObject;
        temp.val = 2;
        return temp;
    }
    public static void main(String [] args){
        MyClass x = new MyClass(4);
        System.out.println(myObject.getVal());
        x.func1();
        System.out.println(myObject.getVal());
    }
}

It prints:

 1
 2

I was expecting it to print:

 1 
 1

There seems to be a fundamental misunderstanding on my part. I was expecting that myObject being a final static value cannot be changed, and when I do MyClass temp = myObject, I create a new object called temp of type MyClass and assign the value of myObject to this newly created object. Please correct me if I am wrong. It seems that the there is no new object created and temp simply points to the original myObject

EDIT: Thanks for the answers! I now understand that the = operator never makes a copy of the object, it just copies the reference. What I need is to make a copy of myObject and store it in temp. What would be the best way to achieve this?

EDIT2: Another strange behavoir or a feature of Java?

I modified the code slightly

class MyClass {
    private Integer val;
    public static final MyClass myObject = new MyClass(1);
    MyClass(int a){
        val = a;
    }
    public int getVal(){
        return val;
    }
    public MyClass func1(){
        MyClass temp = new MyClass(33);
        temp.val = myObject.val;
        temp.val = 2;
        return temp;
    }
    public static void main(String [] args){
        MyClass x = new MyClass(4);
        System.out.println(myObject.getVal());
        MyClass y = x.func1();
        System.out.println(x.getVal());
        System.out.println(y.getVal());
        System.out.println(myObject.getVal());
    }
}

output is

 1
 4
 2
 1

Therefore, when I create temp using new MyClass(33) and then set temp.val = 2, it actually makes a copy of val. In other words, temp.val does not point to myObject.val. Why is this so?

  • 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-15T16:06:11+00:00Added an answer on May 15, 2026 at 4:06 pm
    MyClass temp = myObject;
    

    That’s not creating a new instance, it’s just assigning the reference so that temp points to the same instance as myObject.

    So your statement:

    I create a new object called temp of
    type MyClass

    is incorrect, as you aren’t creating a new object here, just assigning an object reference.

    EDIT
    If your goal is to make a copy of myObject and return it from func1(), then you could do this (by copy I assume you mean copying the val value as well, if you want to use a different value for val then you can adjust this code accordingly):

    public MyClass func1(){
        MyClass temp = new MyClass(myObject.getVal());
        return temp;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I have a private static readonly field in my class: public class MyClass {
I have the following class public class MyClass { private int myAttr; public void
Let's say I have the following class: class MyClass { private: int Data; public:
For example we have code class MyClass { private: int data; public: int getData()
Okay, I have something like this in C++: class MyClass{ private: int someVariable; int
I have a class that looks like the following: public class MyClass { private
In a class, I have a container: public class MyClass implements MyClassInterface { private
I have a List<MyClass> The class is like this: private class MyClass { public

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.