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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:31:04+00:00 2026-06-04T04:31:04+00:00

I have the following two classes: public class Class1 { public Class1 randomvariable; //

  • 0

I have the following two classes:

public class Class1
{
    public Class1 randomvariable; // Variable declared

    public static void main(String[] args)
    {
        randomvariable = new Class1(); // Variable initialized
    }
}

public class Class2
{
    public static void ranMethod()
    {
        randomvariable.getSomething(); // I can't access the member "randomvariable" here even though it's public and it's in the same project?
    }
}

I am very certain that it’s a very fundamental thing I’m missing here, but what am I actually missing? The Class1 member “randomvariable” is public and so is the class and both classes are in the same project.
What do I have to do to fix this problem?

  • 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-04T04:31:05+00:00Added an answer on June 4, 2026 at 4:31 am

    There are two problems:

    Firstly, you’re trying to assign a value to randomvariable from main, without there being an instance of Class1. This would be okay in an instance method, as randomvariable would be implicitly this.randomvariable – but this is a static method.

    Secondly, you’re trying to read the value from Class2.ranMethod, again without there being an instance of Class1 involved.

    It’s important that you understand what an instance variable is. It’s a value associated with a particular instance of a class. So if you had a class called Person, you might have a variable called name. Now in Class2.ranMethod, you’d effectively be writing:

    name.getSomething();
    

    That makes no sense – firstly there’s nothing associating this code with Person at all, and secondly it doesn’t say which person is involved.

    Likewise within the main method – there’s no instance, so you haven’t got the context.

    Here’s an alternative program which does work, so you can see the difference:

    public class Person {
        // In real code you should almost *never* have public variables
        // like this. It would normally be private, and you'd expose
        // a public getName() method. It might be final, too, with the value
        // assigned in the constructor.
        public String name;
    
        public static void main(String[] args) {
            Person x = new Person();
            x.name = "Fred";
            PersonPresenter.displayPerson(x);
        }
    }
    
    class PersonPresenter {
    
        // In a real system this would probably be an instance method
        public static void displayPerson(Person person) {
            System.out.println("I present to you: " + person.name);
        }
    }
    

    As you can tell by the comments, this still isn’t ideal code – but I wanted to stay fairly close to your original code.

    However, this now works: main is trying to set the value of an instance variable for a particular instance, and likewise presentPerson is given a reference to an instance as a parameter, so it can find out the value of the name variable for that instance.

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

Sidebar

Related Questions

Suppose I have the following classes: public class Test { public static void main(String[]
I have the following two classes: public class Address { public string AddressLine1 {
I have the following two classes. public class Foo { public Bar Args {
Let's say I have the following two classes: public class Person { public string
Let's say I have the following two classes: public class TestResults { public string
I have the following two classes, one inherits from the other Class A{ void
I have the following two classes: public class User { public Integer userId; //
Say i have the following two classes: public class User { public int ID
Suppose we have following two classes: class Temp{ public: char a; char b; };
Assume I have the following two classes: public class User : Entity { 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.