Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to create an array through reference

 
Nitin Fresher
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Javaranch.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • 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}

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic