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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:15:55+00:00 2026-06-06T17:15:55+00:00

I have a simple problem that I’ve been stuck on for some time and

  • 0

I have a simple problem that I’ve been stuck on for some time and I can’t quite find the answer to. Basically, I’m creating an object and trying to access the variables without using static variables as I was told that is the wrong way to do it. Here is some example code of the problem. I receive an error in the first class that can not be resolved to a variable. What I would like to be able to do is access t.name in other methods outside of the main, but also in other classes as well. To get around this previously I would use Test2.name and make the variable static in the Test2 class, and correct me if I’m wrong but I believe that’s the wrong way to do it. Any help would be greatly appreciated =)

public class Test {
  public static void main(String[] args) {
    Test2 t = new Test2("Joe");         
  }

  public void displayName() {
    System.out.println("Name2: " + t.name);
  }
}

public class Test2 {
  String name;

  public Test2 (String nm) {
    name = nm;
  } 
}
  • 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-06T17:15:56+00:00Added an answer on June 6, 2026 at 5:15 pm

    I see others have posted code snippets, but they haven’t actually posted why any of this works (at the time of this writing.)

    The reason you are getting a compilation error, is that in your method

    public static void main(String[] args) {
    
        Test2 t = new Test2("Joe");         
    }
    

    Variable t’s scope is just that method. You are defining Test2 t to only be in the main(String[] args) method, so you can only use the variable t in that method. However, if you were to make the variable a field, like so, and create a new instance of the Test class,

    public class Test {
    Test2 t;
    public static void main(String[] args) {
        Test test = new Test();
        test.t = new Test2("Joe");  
       test.displayName();
    }
    
    public void displayName() {
        System.out.println("Name2: " + t.name);
    }
    }
    

    Then you should no longer be getting any compilation errors, since you are declaring the variable t to be in the class Test scope.

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

Sidebar

Related Questions

Here's a simple problem that's been bugging me for some time. I often find
A simple problem that I just can't find the 'right' answer too. I have
I have a simple problem that I have not been able to find an
actually I'm have a very simple problem (that's for sure) I just can't find
I have a pretty simple problem. Basically I have an array called $list that
I have a problem that is quite simple to understand, but for me it
I have a simple problem that I cannot seem to find a solution to.
I have simple LINQ problem that I can't figure out. I have a table
I have very simple problem that I can't solve. I need to do something
I've got a weird problem that I can't understand... I have a simple HelloWorld

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.