• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can the following code throw any exception or stale value?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying me in details. Can the following has the potential to fail or throw an exception?

Class A{

private static B b;

public void setB(B b1){
b = b1;
}

public A(B b1){
setB(b1);
}

public B getB(){
return b;
}
}

If there is only one thread say T1 and it does this following:

Hi,
I have a question on the following situation

1. Create an instance of A say, A a = new A();
2. Then call another method that create another instance of A in a method say create() method as shown below:

void create(){
Class<?> theClassA = Class.forName("A");
A a = (A)theClassA.newInstance();
Class<?> theClassB = Class.forName("B");
B b = (B) theClassB.newInstance();
a.setB(b);
}

Once the thread return from this create() method it calls a.getB() - where a object was created before the method create was called?. Will this be able to see the B? or there is a possibility it can see null reference of B as B object is set in the method created in an unusual way?

regards,
gul
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Although a thread will execute this code, this is not really a threads question -- move to Java in General forum.

Henry
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i don't think it will throw an exception, but i have a hard time reading examples like this that use A and B instead of real examples.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic