• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

declaring and instantiating objects

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
adithi gudipudi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
adithi gudipudi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use braces and drop the size
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
----------------------------------
 
adithi gudipudi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic