Link to home
Start Free TrialLog in
Avatar of futureminds
futureminds

asked on

Instance Variables Question

In a class, how do I declare an array instance variable which uses another class (one that I have previously defined) as its data type?

How will my new class know where to get hold of that data type from the other class, if indeed that is what happens?

Maybe I'm confused, and missed something elementary in my thinking, but if anyone can explain I'd be grateful.
Thanks
futurem.

Avatar of imladris
imladris
Flag of Canada image

I'm not dead certain I understand what you mean, but to get the ball rolling I'll have a stab at it.

Let me answer a simpler question first: How do I declare a variable which uses another class as its data type?

Like so:

Here's the class/data type to be used:
class MyDataType
{   //contains instance variables
    //a constructor and methods
}

Here's the class using it:
class UseClassAsData
{   MyDataType var;

    //also contains constructors
    //and methods
}

How does it know where to find it? Well, that is part of the JVM's job. Basically in Java, the JVM looks for a class file named MyDataType. It might need to be in the current package (if it is not public) or anywhere along the classpath if it is.

So for an instance array you do the same thing except make an array out of it:

class UseClassAsData
{   MyDataType var[];

    UseClassAsData()
    {   var=new MyDataType[5];
        return;
    }
}

Did I clarify anything? Or did I misunderstand your question?
Avatar of Laminamia063099
Laminamia063099

Take the following two classes as examples:

//First class
package a;

class A {

}

//Class that will use A
package b;

import a.A; //this tells the compiler to know use
                 //class A from package a

class B {
  A [] a;
}

Make sure that both A is in your classpath when you compile, and the compiler will take care of the rest for the compilation.  When you run your program, make sure that both classes are in your classpath as well, and the interpreter will know from where to get the code.

If neither classes use packages, then just make sure that they are in your classpath, and you don't need to use the package or import statements.

Laminamia :)
Ooops.  Sorry imaldris :)

I think that is exactly what he's looking for.

Laminamia :)
Avatar of futureminds

ASKER

imladris:

I think you understood my question perfectly; it's me who didn't, because some concept got in the way, and its residue is still there: this is your constructor, right ? ..

UseClassAsData()
    {   var=new MyDataType[5];
        return;
    }

 ... why the "5"? I'm sure I've just asked a silly question, but whilst the simple non-array answer I understand, this bit I'm not sure I do.

Thanks, and thanks laminamia again also;

ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The size of the array is a fixed one once it has been constructed.It cant grow in size as like vector.The java will expect the array size to be declared at the runtime.It's a must.you can use the vector instead of array if you expect it to grow.you can even have objects of different class stroed in the vector.
Perhaps another note needs to be added. When you declare:

MyDataClass var;

You get a variable (var) that can reference an object of type MyDataClass, but get no instance of the class itself. To get an actual instance of the class you must:

var=new MyDataClass();

In the same way if you declare:

MyDataClass var[];


var is now a reference to a (nonexistent) array of references to MyDataClass.

When you allocate the array with:

var=new MyDataClass[5];

You now have an array (referenced by var) that contains 5 elements. Each element is a reference to a (non-existent) object of type MyDataClass. To get actual instances of MyDataClass you would have to create them; maybe in a loop something like:

for(int i=0; i<5; ++i)
{   var[i]=new MyDataClass();
}
Adjusted points from 50 to 70
Yep, that's crystal imladris.