Static class fields in construction - java

// package and import things..
public class A {
public int x;
public static A ob;
A() {
A.ob.x = 5;
}
public static void main(String args[) {
A.ob = new A();
System.out.println(ob.x);
}
}
Why this code is give NullPointerException ? If i change "A.ob.x" to "this.x", it's done. But already A.ob = this in this code?

A.ob = new A();
This first creates an A by executing the constructor, and then, assigns the created A to A.ob. It's basically equivalent to
A tmp = new A();
A.ob = tmp;
So, at the time the constructor is called, A.ob is still null. So you get a NullPointerException.

Related

copy constructor clarification needed

public class CopyConstructorEx
{
String web, webb;
CopyConstructorEx(String w){
web = w; }
CopyConstructorEx(CopyConstructorEx je){
webb = je.web; }
void disp(){
System.out.println("Website: "+web); }
public static void main(String args[]){
CopyConstructorEx obj1 = new CopyConstructorEx("BeginnersBook");
CopyConstructorEx obj2 = new CopyConstructorEx(obj1);
obj1.disp();
obj2.disp();
}
}
output:
Website: BeginnersBook
Website: null
Can anyone explain why second output is null?
web being a string type variable is null by default. In your copy constructor, you aren't assigning anything to it, so there's no reason for it to change.

What would happen if an object is created in non-static method of class in java?

class Data {
int x = 20; // instance variable
public void show() // non-static method
{
Data d1 = new Data(); // object of same class Data
int x = 10;
System.out.println(x); // local x
System.out.println(d1.x);// instance variable x
}
public static void main(String... args) {
Data d = new Data();
d.show(); // "10 20"
}
}
So my question when an object is created in show() namely 'd1' , it must have its own set of data members and must have allocated a new stack for its show() method , which in return should create a new object and thus the cycle should go on and stack-overflow occurs??
But this is working perfectly fine??
Data d1=new Data();
this statement itself will not allocate a new stack for show(). show()'s stack is allocated only when it gets called, for example, when it gets called in main.
Your show() method will be called only once. show() is NOT called when an instance of Data is being created. So, you will not get SOE.
Try this code to get SOE :P
class Data {
int x = 20; // instance variable
Data() {
show(); // This will cause SOE
}
public void show() // non-static method
{
Data d1 = new Data(); // object of same class Data
int x = 10;
System.out.println(x); // local x
System.out.println(d1.x);// instance variable x
}
public static void main(String... args) {
Data d = new Data();
d.show(); // "10 20"
}
}

Unable to create new object

I created these two files in java and they don't compile. This error comes up:
cannot find symbol C02FootprintV1".
Why doesn't the program recognize the object? I am new to this.
How could I fix this problem?
public class CO2FootprintV1 {
private double myGallonsUsed;
private double myTonsCO2;
private double myPoundsCO2;
CO2FootprintV1(double gals) {
myGallonsUsed = gals;
}
public void calcTonsCO2() {
myTonsCO2 = myGallonsUsed * 0.878;
}
public double getTonsCO2() {
return myTonsCO2;
}
public void convertTonsToPoundsCO2() {
myPoundsCO2 = myTonsCO2 * 220462262;
}
public double getPoundsCO2() {
return myPoundsCO2;
}
}
public class CO2FootprintV1Tester {
public static void main(String[] args) {
double gals;
double tonsCO2, poundsCO2;
gals = 1300;
CO2FootprintV1 object = new C02FootprintV1(gals);
object.calcTonsCO2();
tonsCO2 = object.getTonsCO2();
object.convertTonsToPoundsCO2();
poundsCO2 = object.getPoundsCO2();
}
}
On the line
CO2FootprintV1 object = new C02FootprintV1(gals);
you have C02 (see zero two) on the right hand side, you meant for it to be
CO2FootprintV1 object = new CO2FootprintV1(gals);
or CO2 (see oh two). Also, you should consider that the error messages your tools give you might be correct.
Just change:
CO2FootprintV1 object = new C02FootprintV1(gals);
to:
CO2FootprintV1 object = new CO2FootprintV1(gals);
That's why it is important to have good naming practice.
You put a "0" (cero) instead of an "O" (letter):
CO2FootprintV1 object = new C02FootprintV1(gals);
Try this:
CO2FootprintV1 object = new CO2FootprintV1(gals);

How do I correctly instantiate a public class+data structure in a class so that other objects can use it?

In my code, I have a seperate Runner class that instantiates a World, which has a 4x4 array of Locations (a separate class) stored as a Location[][] array. When I print/try to use the Location array, its value is null, and it throws a NullPointerException.
public class Runner
{
public static void main(String[] args)
{
...
WumpusWorld test_loc = new WumpusWorld();
System.out.print(test_loc) //This prints an ID for the WumpusWorld object
System.out.print(test_loc.world) //Null value prints here
//I'd like to pass the test_loc.world values to an actor here
...
}
}
The applicable code for the WumpusWorld is as follows:
public class WumpusWorld
{
public Location[][] world;
public WumpusWorld()
{
new WumpusWorld((byte) 4); //this constructor is used
}
...
public WumpusWorld(byte size)
{
this.world = new Location[size][size];
for(byte i = 0; i<size; i++)
{
for(byte j = 0;j<size;j++)
{
world[i][j] = new Location(j,i,true,false,false);
}
//Location instances called in the form world[x][y]
//are error free in constructor
...
}
}
Your problem might be in the way you call public WumpusWorld(byte size) from the default constructor.
Try this:
public WumpusWorld()
{
this((byte) 4);
}
With new in the call, I had uninitialized values in the inner class

Initialize Empty HashMap

I come from writing a lot of JavaScript, so bear with me.
I've got 3 HashMaps, which i reference in a method in a different class. My code (very simply) looks like so:
public class MainClass {
private HashMap<String,Nation> randomHashMap = new HashMap<String,Nation>();
DifferentClass d = new DifferentClass(this);
} //with getters/setters
public class DifferentClass {
private MainClass mc;
public void randomMethod() {
System.out.println("randomHashMap is " + (mc.getRandomHashMap() == null));
} //returns null
public DifferentClass(MainClass c) {
this.mc = c;
}
}
However, when I call them in my other method, they're null.
How do I create a new, empty HashMap?
You need to initialize your MainClass mc variable before using it in the DifferentClass#randomMethod method. Also, make sure you're using the mc variable instead of the MainClass.getRandomHashMap() method (by your actual code, we don't know how it behaves). Your code will look like this:
public class DifferentClass {
private MainClass mc = new MainClass();
public void randomMethod() {
//assuming getRandomHashMap is the getter of randomHashMap attribute (and non static)
System.out.println("randomHashMap is " + (mc.getRandomHashMap() == null));
}
}
public class MainClass {
private HashMap<String,Nation> randomHashMap = new HashMap<String,Nation>();
DifferentClass d = new DifferentClass(this);
public HashMap<String,Nation> getRandomHashMap() {
return this.randomHashMap;
}
} //with getters/setters
The code you posted is in fact perfectly all right as far as field initialization. I made an SSCCE from it with minimal intervention:
class Nation{}
public class MainClass {
private HashMap<String,Nation> randomHashMap = new HashMap<String,Nation>();
DifferentClass d = new DifferentClass(this);
public Object getRandomHashMap() {
return randomHashMap;
}
public static void main(String[] args) {
new MainClass().d.randomMethod();
}
} //with getters/setters
class DifferentClass {
private MainClass mc;
public void randomMethod() {
System.out.println("randomHashMap is " + (mc.getRandomHashMap() == null));
} //returns null
public DifferentClass(MainClass c) {
this.mc = c;
}
}
and it prints
randomHashMap is false
which proves that randomHashMap is indeed non-null.

Categories