| 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
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
"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 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: Unable to create an array through reference
|
|
|