aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes declaring and instantiating objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "declaring and instantiating objects" Watch "declaring and instantiating objects" New topic
Author

declaring and instantiating objects

adithi gudipudi
Greenhorn

Joined: Oct 20, 2003
Posts: 25
hello All,
I have a class Item to and I want to create array of this.
If I do
Item items[3];
items[0]= new Item();
.....
items[0].setItemID(1);
....
is this not correct...
Please help
Thanks
adithi
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Welcome to JavaRanch, adithi!
You can't declare an array specifying its size like this: Stuff myStuff[n];
If you change it to

it should work. Or, in two lines:

[ October 20, 2003: Message edited by: Vad Fogel ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by adithi gudipudi:

Item items[3];
items[0]= new Item();

You're close, but you actually have to create the array object, not just declare it. Like this:

I hope that helps.


SCJP Tipline, etc.
adithi gudipudi
Greenhorn

Joined: Oct 20, 2003
Posts: 25
Thanks for the quick replies..But
if I do like this...
Item[] items = new Item[3];
items[0]= new Item("ItemID1","ItemName1");
it gives me compiler error
why so?
Thanks
adithi
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
It's hard to say why without seeing the code. Maybe, you're not matching the Item constructor's arguments, maybe, Item class is not visible from where you wanna create it, maybe... 1000 and one reasons. Examine the compiler error carefully, it must have a good hint.
adithi gudipudi
Greenhorn

Joined: Oct 20, 2003
Posts: 25
Hi I get this message when I compile it..
First.java:13: ']' expected
items[0]= new Item("ItemID1","ItemName1");
^
First.java:13: <identifier> expected
items[0]= new Item("ItemID1","ItemName1");
^
First.java:13: cannot resolve symbol
symbol : class emp
location: class First
items[0]= new Item("ItemID1","ItemName1");
^
3 errors
I have declared a constructor Item(String ID, String Name) so this should work I assume...
Thanks
Lakshmi.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
If you post your code, we'll see the error clearer. Make sure class emp is accessible from class First, make sure that the array of Items is created inside the scope of the assignment.
Anish Doshi
Greenhorn

Joined: Jun 13, 2003
Posts: 23
Hey,
The problem is dat u have just created an array refrence but u have'nt created and array object yet. i mean u have done this:
Item items[3]; \\ Array refrence created but wont compile coz u \\shouldn't specify lngth der
Item items[] = new Item[3]; \\ Array Object created.
Untill u do not create an array object u would not b able to use d array itself. Then u can create ur object like:
items[0] = new Item; \\ Your object created.
Then u can use it like:
items[0].setItemID(1);
I hope diz mght help you. Bye.
Kind Regards,
Anish Doshi.


Sun Certified Programmer for Java platform 1.4
adithi gudipudi
Greenhorn

Joined: Oct 20, 2003
Posts: 25
Hi all,
This is my program.
I want to create an Array of Item and use the constructor I have ( 2 parameters ).

------------------------
public class Temp
{
public static void main(String args[])
{
Item item1= new Item(1,"Item 1");
System.out.println(" the Item I details are "+ item1.getItemID()+".\n The info is "+item1.info);
}
}

class Item
{
private int ItemID;
public String info;

public Item(int ID, String info)
{
ItemID= ID;
this.info=info;
}


public int getItemID()
{
return ItemID;
}
}
----------------
I want to add some thing like
Item[] items= new Item[3]; but I want to initialize the elements also to different values. I checked different books but could find no information about this.
Thanks much
Adithi.
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168
Use braces and drop the size
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104
check this ...
----------------------------------
public class TempItem
{
public static void main(String args[])
{
Item arrItem[] = new Item[3];
for(int i=0;i<arrItem.length;i++)
{
arrItem[i] = new Item(i,"Item "+i);
System.out.println(" the Item "+ i +" details are "+ arrItem[i].getItemID()+".\n The info is "+arrItem[i].info);
}
}
}

class Item
{
private int ItemID;
public String info;
public Item(int ID, String info)
{
ItemID= ID;
this.info=info;
}

public int getItemID()
{
return ItemID;
}
}
----------------------------------


Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
adithi gudipudi
Greenhorn

Joined: Oct 20, 2003
Posts: 25
Thanks Michael,
One more questions, If I dont declare the constructor and want to use the default one.
then saying
Item items = new Item[3];
...
would do all the work right? I dont have to do anything else.
Thanks again for you help
adithi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: declaring and instantiating objects
 
Similar Threads
how to easily sort the same TreeSet few times?
Simple Bean question
Access to logic:iterate
Recursive program help
tag to loop a List