Solved

Error in getSize() getter - creating a size property that is an int array of height and width

Posted on 2007-03-27
5
304 Views
Last Modified: 2010-03-31
Experts:

I'm trying to create a size property that is an int array of height and width:

      private int [] size = new int [2];

      public int [] getSize() {  // getSize() is underlined in red "here is my error"
            size[0] = height;
            size[1] = width;
            return size;
      }

      public void setSize(int h, int w) {
            height = h;
            width = w;
      }

When I hover the cursor over the getSize() it pops up the following message:  
The return type is incompatible with Component.getSize()

I've gone through all of my code looking for incompatibilities and I can't find any.  So, I'm beginning to think there is something wrong with my syntax.

Any ideas?
Thanks,
Patricia
0
Comment
Question by:pdlarue
  • 3
5 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 40 total points
ID: 18801764
return a type of java.awt.Dimension
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 40 total points
ID: 18801783
public Dimension getSize() {
      return new Dimension(width, height);
}
0
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 20 total points
ID: 18801786
Or, change your getSize method name. It is conflicting with the getSize method in Component, and you can't have a method with the same name and parameters but a different return type.
0
 
LVL 10

Accepted Solution

by:
ADSLMark earned 40 total points
ID: 18801820
Explanation:

You are trying to override a method from a superclass with the name: "getSize". Since this method returns a Dimension, you cannot define your own method "getSize" with the same type of parameters but a different return type.
Imagine the situation where you have:

int[] getSize(int width, int height);

and

Dimension getSize(int width, int height);

Now if i were to call getSize(800,600); which method should i choose? Java is not smart enough to know this, so it doesnt allow you to have these two functions simultaneous. Simple solutions are returning the correct type or changing the method name a bit, like getMySize(..).

Good luck,
Mark
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18801964
:-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now