• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unable to create an array through reference

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Pets{
String name;
public void bark(){
System.out.println(name + " says Ruff !!!");
}
public static void main(String [ ] args){
//setting up pet object
Pets pets = new Pets();
//accessing pet object
pets.bark();
pets.name = "Jimmy";
pets.bark();
//setting up pet array
Pets petArray = new Pets[3];
}
}

Why couldn't it not make an array of Pets
C:\>javac Pets.java
Pets.java:14: incompatible types
found : Pets[]
required: Pets
Pets petArray = new Pets[3];
^
1 error
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code. It will highlight your code which makes it much easier to read.

The correct syntax is:
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Javaranch.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitin Fresher wrote:class Pets{


Why couldn't it not make an array of Pets
C:\>javac Pets.java
Pets.java:14: incompatible types
found : Pets[]
required: Pets
Pets petArray = new Pets[3];
^
1 error




As you see compiler is complaining "incompatible type", means LHS and RHS are incompatible.
What is LHS ? ==> Pets { an Object }
What is RHS> ==> Pets[] {an Array of Pets}

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"LHS" and "RHS" mean "left hand side" and "right hand side". The refer to what's on the left and right sides of an operator (in this case, the == operator) in an expression.

Sunny: please don't use abbreviations like that without explaining what they mean, they make your post harder to understand, especially for beginners who haven't encountered those terms before.
 
reply
    Bookmark Topic Watch Topic
  • New Topic