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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:27:24+00:00 2026-06-12T22:27:24+00:00

In my class B, I want to be able to use methods from my

  • 0

In my class B, I want to be able to use methods from my class A. This is what I have tried.

  1. Imported class A
  2. initialized it – classA a = new classA(null, null);
  3. The added to my methods where I want to use a class A method

    public void getAMethod() {
    a.getTestValue;
    }

My questions are:

  1. Is this the correct way to do it?
  2. Is every time I execute class B, is it always creating a new instance of class A?
  3. If class A is opened and I run class B, does class B get the current values of variables in class A, or does it get the class A defaults because of new classA(null, null)?

My issues are:

  1. I am getting 2 different values for the same variable.

Example: Class A –

initialize = boolean test = false

When class A dialog opens , set test = true

Create method

public boolean getTestValue() {
   return test;
}

From within class A when I run getTestValue, it is always true
From class B when getTestValue is called, it always returns false (even with the class A dialog open)

It seems like that class b is making a new instance of class A every time, so it only sees the default values of class A. When class B calls class A, I need to get the current values of class A, not the defaults

EDIT
I changed the method in Class A to static and it fixed the issue I was having. This was a big help to me – Thanks to all for the suggestions!

  • 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-12T22:27:26+00:00Added an answer on June 12, 2026 at 10:27 pm

    Okay, there’s a fair amount of ground to cover, but I’ll try to be brief and answer each question in turn.

    1. Is this the correct way to do it?

    You don’t typically need to import classes in a small program. If ClassA and ClassB are inside the same package, or they both do not have a package assigned, but reside in the same directory, then no importing is required.

    As far as wrapping ClassA methods in ClassB methods, no, no. You can access a ClassA static method from within ClassB at any time by doing;

    ClassA.myMethodInClassA();
    

    If myMethodInClassA(); is not static, then you need to call it on an instance of ClassA so, (still in ClassB) you would do;

    ClassA myA = new ClassA();
    myA.myMethodInClassA();
    

    Which brings me on to static vs non static fields. If your fields initialize and test are marked as static, such as;

    public static boolean initialize = false;
    

    Then they will always have the same value across all instances of ClassA and can be access from anywhere with;

    if(ClassA.initialize) {
        //something
    }
    

    If they are not marked as static, then again (much like the non-static methods) you need an instance of ClassA with which to query the fields such as;

    ClassA myA = new ClassA();
    if(myA.initialize) {
        //something
    }
    

    And in this case, each instance of ClassA has its own copy of initialize, so the value of initialize depends on which instance you are querying.

    2. Is every time I execute class B, is it always creating a new instance of class A?

    If you call a constructor for ClassA inside the constructor for ClassB then the answer is yes, in other words if you do;

    public ClassB() {
        //constructor for ClassB
        ClassA c = new ClassA();
    }
    

    Then yes, you will create a new ClassA every time you create a new ClassB.

    3. If class A is opened and I run class B, does class B get the current values of variables in class A, or does it get the class A defaults because of new classA(null, null)?

    If by ‘opened’ you mean you ran java ClassA and then ran java ClassB in your command prompt, then what you have there is two separate programs, two instances of the JVM running in parallel. These two programs have nothing to do with one another, and cannot communicate with each other in any way unless you specifically implement something.

    If by ‘opened’ you mean constructed, such as new ClassA(); then we’re talking about class vs instance members again.

    I ranted on about static vs non-static fields and methods in my answer to question 1, which covers this ground. Overall I would advise you to read some docs, such as:

    http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

    TLDR: Go read this link ^^

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

Sidebar

Related Questions

Depending on where I use my Class, I want to be able to show
So I want to be able to add/remove class methods at runtime. Before you
I have a C++ class MyObject and I want to be able to feed
I derived ImageButton from Button class. I want to be able to set text
I want to define a class that is able to populate itself reading from
I have a base class that has methods that use a generic type in
I want to be able to use methods that I haven't required() at the
I'm creating a abstractFactory class and I want to be able to send the
I'm writing a matrix class and I want to be able to cast a
How do I override a class special method? I want to be able to

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.