I am trying to update a jlabel from another class. I've pasted my code below.
Class A {
public void setNetAmount(String s){
jLabel51.setText(s);
}
public void setDis_percentage(String s){
jLabel53.setText(s);
}
public void setDiscount(String s){
jLabel55.setText(s);
}
public void setAdjustment(String s){
jLabel57.setText(s);
}
}
Class B{
public void SetData(){
new A.setNetAmount(""+netAmount);
new A.setDis_percentage(""+dis_percentage);
new A.setDiscount(""+discount);
new A.setAdjustment(""+adjustment);
}
}
I am calling the SetData() method in Class A.
public void getData(){
B b = new b();
b.setData();
}
Is there anything wrong with my code ? It is not working at all. Is there any issue of EDT? Please help.
You can't keep invoking "new A". This creates a new instance of class A.
Not really sure why you have a class B to invoke a few methods from class A, but if you use this approach then you would need to pass a reference of class A the your class B method.
Something like:
public class B
{
public void setData(A a)
{
a.setAmount(...);
a.setPercentage(...);
...
}
}
Then when you invoke the method in your class A you would use:
B b = new B();
b.setData(this);
Although this is a really strange design.
Related
class A
{
class B b;
B b = new b();
}
class B extends A
{
b.function();
}
Here can B use the same object created in A?
Following is your program:
class C {
public String cvariable;
public void cfunction(){
System.out.println("string");
}
}
class A {
public C c1;
public void funtiona(){
c1 = new C();
}
}
public class B extends A {
public void functionb(){
c1.cfunction();
}
public static void main(String args[]){
B b = new B();
b.functionb();
}
}
It is correctly throwing null pointer exception. It proceed as follows:
In the main method you call functionb()
In functionb() you call cfunction() with c1, but c1 is just an variable of type C(as not initialized yet) which contains null. So getting null pointer exception.
See the following program, It will throw java.lang.StackOverflowError
class B{
A a = new A();
public B(){
System.out.println(a.hashCode());
}
}
public class A extends B{
public void show(){
a.hashCode();
}
public static void main(String[] args){
new A().show();
}
}
This is because program goes in the infinite loop, As before creating a child class object it calls the parent class constructor and in parent class for hash code it again calls the child class constructor. so an infinite loop
I'm having several problems with the following code:
private ArrayList<TemperatureSensor> sensors = new ArrayList<TemperatureSensor>();
//[...]
private double getAverageTemperature() {
double addition = 0;
for(TemperatureSensor sensor : this.sensors)
addition += sensor.getTemperature();
return addition / this.sensors.size();
}
}
The method getTemperature is in the class TemperatureSensor, and it's defined as this:
public int getTemperature() {
// random number [5,40]
return new Random(System.currentTimeMillis()).nextInt(36)+5;
}
In the getAverageTemperature() method I'm getting an error saying that "The method getTemperature() is undefined for the type TemperatureSensor"
Also, how do I call a specific method that two classes have, and their objects are stored in an ArrayList, from an abstract class?
Thanks in advance.
The code looks ok. Does everything compile? Is everything freshly compiled? From what you describe I'd assume there is an old version of the class on your classpath that does not yet have that method.
Clean and compile you code once again as code looks fine. For your second question, find example below
public abstract class A {
public abstract void method();
}
public class B extends A{
public void method(){
//code
}
}
public class C extends A {
public void method(){
//code
}
}
public class Main{
public static void main(String args[]){
ArrayList<A> obj=new ArrayList<A>();
obj.add(new B());
obj.add(new C());
for (A a : obj) {
a.method(); // it will call respective method of B and C class
}
}
}
In Java, I have something like this
public class A {
private String title;
public A () {
// do something
}
public void run () {
B b = new B();
b.run();
}
public void changeTitle(String newTitle) {
this.title = newTitle;
}
}
public class B {
public B() {
// do something
}
public void run() {
}
}
My question is in method run() of B, is it possible to invoke method changeTitle() in A to change the title of the instance of A that instantiates B?
Thanks
B can only invoke methods on A if it contains a reference to an instance of A. You could pass an instance of A into B to achieve this.
public void run () {
B b = new B(this);
b.run();
}
public class B {
private A a;
public B(A a) {
this.a = a;
a.changeTitle("Ha!");
}
}
if B accepts a type A in its constructor, and when you say new B, pass in 'this' similar to
public void run () {
B b = new B(this);
b.run();
}
now you have a copy of the A object your working with.
Sure. Pass an instance of A in the constructor for B.
This will only be possible if you pass this as argument when you invoke B's run() method.
Not unless you arrange for instances of B to know what instance of A they should do that to (e.g., by passing that into the constructor for B and remembering it).
If you're doing something like this then you should consider, e.g., what you want to happen if an instance of B is created by something other than an instance of A.
no. As such an instance of B does not know who created it.
However with B as:
public class B {
private A creator;
public B(A creator) {
this.creator = creator;
// do something
...
}
and creating new B(this) in A.run(), B could call creator.changeTitle("whatever") in its run() method.
Is B is a non static inner class of A you van invoke the mehods of A (and access its fieldes).
In the code below, myString is always initialized to null. I have to manually initialize in an init() or similar. As far as I can tell it is related to superclass/subclass but I don't understand the exact mechanism
public class A extends B {
private String myString = "test";
public static void main(String[] args) {
new A();
}
public A() {
super();
}
public void c() {
System.out.println(myString);
}
}
public class B {
public B() {
c();
}
public void c() {
}
}
The issue with your code is, that myString is initialized at the begin of the constructor of class A but right after the super constructor (i.e. class B). Since you access the variable before from the constructor of class B (indirectly via call to overriden methode c) your get this behaviour.
As a rule of thumb: if you want to avoid unexpected behavior do not call overriden methods before the constructor has been executed.
Add a call to c(); overidden method right after the object has been fully created and call to superclass constructor is done.
Change your code to this ..
public class A extends B {
private String myString = "test";
public static void main(String[] args) {
new A();
}
public A() {
super();
c(); // Include the call to c(); here ...
}
public void c() {
System.out.println(myString);
}
}
public class B {
public B() {
}
public void c() {
}
}
// Output : test
Thinking in Java Second Edition by Bruce Eckel, Behavior of polymorphic methods
inside constructors (p. 337-339).
I'm new to Java, and I've read over some tutorials on overriding methods, but an example I'm looking at isn't working the way I expect. For example, I have the code:
public class A{
public void show(){
System.out.println("A");
}
public void run(){
show();
}
public static void main( String[] arg ) {
new A().run();
}
}
public class B extends A{
#Override
public void show(){
System.out.println("B");
}
}
When I instantiate and call B.run(), I would expect to see "B" outputted. However, I see "A" instead. What am I doing wrong?
Edit: Yes, the classes are in two separate files. They're shown together for brevity.
Edit: I'm not sure how B is being instantiated, as it's being done by a third-party program using a classloader.
Edit: More info on the third-party program. It starts by calling A.main(), which I didn't initially show (sorry). I'm assuming I need to make "new A().run();" more generic to use the name of the current class. Is that possible?
That code will output B if you:
(new B()).run();
Whatever the problem is, it's not in the code you've quoted.
Updated (after your edit)
If the third-party program is calling A.main(), there's nothing (reasonable) you can do in B that will inject itself into A. As long as A.main is doing new A().run(), it's going to have an instance of A, not an instance of B. There's no "current class name" to use, or if there is (depends on your point of view), it's A, not B.
You'll have to get the third-party program to call B in some way, rather than A, or just modify A directly (e.g., getting rid of B entirely). You do not want to modify A to make it use B; that tightly binds it to a descendant and makes the separation between them largely pointless.
Hope that helps.
I tried, putting your two classes in two files, and it worked nicely, outputting "B". I called :
B b = new B();
b.run();
UPDATED : Also works as (because it is the same runtime instance):
A a = new B();
a.run();
Works for me.
Here's my code for A and B:
package so;
public class A{
public void show(){
System.out.println("A");
}
public void run(){
show();
}
}
class B extends A{
#Override
public void show(){
System.out.println("B");
}
}
Here's my entry point:
package so;
public class EntryPoint {
public static void main(String[] args) {
B b = new B();
b.run();
}
}
It prints out 'B'.
It depends of instantiating. Try this:
A v1 = new A();
A v2 = new B();
B v3 = new A();
B v4 = new B();
v1.run()
v2.run()
v3.run()
v4.run()
I tried your example and my output was B.
How are you instantiating? Here's the exact code I ran.
public class Test {
public static class A {
public void show() {
System.out.println("A");
}
public void run() {
show();
}
}
public static class B extends A {
#Override
public void show() {
System.out.println("B");
}
}
public static void main(String args[]) {
A a = new B();
a.run();
}
}
If your external program instantiates A, you will have A, not B.
But you can try something like this, using some reflection, and pass "com.mypackage.A" or "com.mypackage.B" as arguments to your program.
With this code (exception catch missing), you will be able to print "A" or "B" depending on the string parameter that you pass.
public static void main( String[] arg ) {
String className = arg[0];
Class myClass = Class.forName(className);
Constructor cons = myClass.getConstructor(new Class[0]);
A myObject = (A) cons.newInstance(new Object[0]);
myObject.show();
}