General Java Questions I
Dog[] dog = new Dog[100];
or does it take the memory when I actually create the Dogs in the array eg:
for(int i = 0;i
Answer: The statement above is actually two fold. It is the declaration and
initialisation of the array. Dog[] dog is the declaration, and all this does is declare a
variable of type Dog[], currently pointing to null.
You then initialise the array with new Dog[100], which will create 100 elements in the
array, all of them referencing null.
It is important to realise that the elements of an array are not actually objects, they
only reference objects which exist elsewhere in memory. When you actually create
the Dog objects with new Dog(), these objects are created somewhere in memory
and the elements in the array now point to these objects.
Pedant point:
Nothing ever points to null. It is a constant that represents the value of a reference
variable that is not a pointer to some object new Dog[100] creates an array of 100
null Dog references.
Q: How do I return more than one value using the return command?
Answer: You could make a new object/class that contains these two values and
return it. For example:
Define an object like this:
class MyObj {
public int myInt;
public double myDouble;
}
Then, in your method create one of these, set the corresponding values,
and return it.
MyObj yourMethod() {
MyObj obj = new MyObj()
obj.myInt = 20;
obj.myDouble = 1.0003
return obj;
}
Q: How do I use object serializtion for an object that has other objects as data
member? Do both the class need to implement serialize?
How about static data?
class A{
}
class B{
public A a;
}
Answer: Both the object and all the object references it contains need to belong to
classes that implement Serializable.
Static and transient fields are not serialized. For more, see,
http://java.sun.com/docs/books/tutorial/essential/io/serialization.html
file:///F|/a_jsite/350_tips/general_java I.htm (6 of 33) [2001 07 08 11:24:51]
footer
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved