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

  • Home
  • SEARCH
  • 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 7651347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:28:58+00:00 2026-05-31T11:28:58+00:00

I have a question, now i have two classes as following public class mainClass

  • 0

I have a question, now i have two classes as following

public class mainClass {
    public static void main(String[] args){
        String st1="st1";
        String st2= "st2";

        System.out.println(st1);
        System.out.println(st2);
        testClass t1=new testClass(st1);
        testClass t2=new testClass(st2);
        System.out.println(t1.getString());
        System.out.println(t2.getString());
    }

}
public class testClass {
    static String testString1;
        public testClass(String a ) {
            testString1=a;
        }
        public String getString(){
            return testString1;
        }
}

output

st1
st2
st2
st2

shouldn’t output be the following?

st1
st2
st1
st2

since I created two instances of testClass, each has different input, why am i getting same output??

  • 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-31T11:29:00+00:00Added an answer on May 31, 2026 at 11:29 am

    Your testString1 variable in testClass is declared static, so it will hold the same value across all instances of testClass.

    If you want testString1 to hold a unique value for each instance of testClass that gets created, then you need to remove the static declaration.

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

Sidebar

Related Questions

I have two classes named ROAD and PATH public class ROAD { public string
I have two classes, ThreadItem and Enquiry. public class ThreadItem { [Key] public int
I have two classes in short here they are: public final class ServerMain {
I am altering the question now! I have the search working using the following:
Following on from this question I now have code that can attach to a
I have a two classes, a controller called AppController and a class called URLDelegate
I am concreting a question I had earlier. I have two classes in C++
Consider the following Java function: public void foo(Class<? extends Exception> cl, List<? extends Exception>
I have the following code with private static members. All of these classes say
I have a question regarding static members and functions of non static classes. I

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.