Reputation: 13
I apologize for the mistakes as this is my first post here. This is a basic question but I do not understand the answers I've seen. In the following Java code:
public class RAPVAR_XDATA_TYPE{
public class datacfg{
public int datacfg_len;
public float datacfg_val;
}
public class numdata{
public int numdata_len;
public float numdata_val;
}
public class strdata{
public int strdata_len;
public String strdata_val;
}
}
Why can I not access numdata_len in the following fashion:
RAPVAR_XDATA_TYPE.numdata.numdata_len = 1;
RAPVAR_XDATA_TYPE rxt = new RAPVAR_XDATA_TYPE();
rxt.numdata.numdata_len = 1;
I am trying to build a data structure to mimic the same structure I did in c++ a while ago.
Upvotes: 1
Views: 74
Reputation: 2153
You need to consider that what you have in a class is a 'pattern' for the the data elements an instance of the class will contain. in your example you also have an inner class which is just a nested 'pattern'. Even if you new the outer class you have not created space for the item in the inner class. you would need a constructor that new-ed (instantiated) the inter class objects and set references to them in the instance of your outer class to have something 'like' what you had in c++.
Upvotes: 0
Reputation: 285450
You would need to make the inner classes and their fields static for this to work, but before you do this, don't. Don't try to make C++ structures with Java as that's not the Java way, and will lead to poorly constructed Java programs. Instead get a good book on OOP and Java such as "Thinking in Java" and a book on design patterns such as the GoF book or the Head First book, and learn the Java "way".
Upvotes: 1