Solved

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

Posted on 2007-03-27
5
316 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

679 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