aspose file tools*
The moose likes Beginning Java and the fly likes Unable to create an array through reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unable to create an array through reference" Watch "Unable to create an array through reference" New topic
Author

Unable to create an array through reference

Nitin Fresher
Greenhorn

Joined: Jan 29, 2011
Posts: 1
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

Joined: Oct 25, 2008
Posts: 2700

Please UseCodeTags when posting code. It will highlight your code which makes it much easier to read.

The correct syntax is:


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And welcome to the Javaranch.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

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}


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14428
    
  23

"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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to create an array through reference