In my class B, I want to be able to use methods from my class A. This is what I have tried.
- Imported class A
- initialized it – classA a = new classA(null, null);
-
The added to my methods where I want to use a class A method
public void getAMethod() {
a.getTestValue;
}
My questions are:
- Is this the correct way to do it?
- Is every time I execute class B, is it always creating a new instance of class A?
- 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:
- 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!
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
ClassAandClassBare 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
ClassAmethods inClassBmethods, no, no. You can access aClassAstatic method from withinClassBat any time by doing;If
myMethodInClassA();is not static, then you need to call it on an instance ofClassAso, (still in ClassB) you would do;Which brings me on to
staticvsnon staticfields. If your fieldsinitializeandtestare marked asstatic, such as;Then they will always have the same value across all instances of
ClassAand can be access from anywhere with;If they are not marked as static, then again (much like the non-static methods) you need an instance of
ClassAwith which to query the fields such as;And in this case, each instance of
ClassAhas its own copy ofinitialize, so the value ofinitializedepends 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
ClassAinside the constructor forClassBthen the answer is yes, in other words if you do;Then yes, you will create a new
ClassAevery time you create a newClassB.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 ClassAand then ranjava ClassBin 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
staticvsnon-staticfields 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 ^^