Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Java experts: Why won't this line compile?

Thread view: 
AdamB - 08 Jan 2006 08:01 GMT
Why won't this line compile?:
super(parent instanceof Dialog?(Dialog)parent:(Frame)parent);

The compiler complains with this:

C:\tmp>javac Hmm.java
Hmm.java:10: cannot find symbol
symbol  : constructor JDialog(java.awt.Window)
location: class javax.swing.JDialog
               super(parent instanceof
Dialog?(Dialog)parent:(Frame)parent);
               ^

Here's the line in context:

Hmm.java:

import java.awt.Window;
import java.awt.Dialog;
import java.awt.Frame;
import javax.swing.JDialog;

public class Hmm extends JDialog
{
    public Hmm(Window parent)
    {
        super(parent instanceof Dialog?(Dialog)parent:(Frame)parent);
    }
}

In my mind this should work because the JDialog constructor has
overloads for both Dialog and Frame.  (Both derive from Window.)

Any insight you can give would be appreciated.
- AdamB
zero - 09 Jan 2006 12:58 GMT
"AdamB" <cruxic@gmail.com> wrote in news:1136666529.039781.110900
@g43g2000cwa.googlegroups.com:

> Why won't this line compile?:
> super(parent instanceof Dialog?(Dialog)parent:(Frame)parent);
[quoted text clipped - 31 lines]
> Any insight you can give would be appreciated.
> - AdamB

A similar question has come up recently.  In JDK 1.4 the ternairy
operator won't compile at all because Dialog and Frame are not directly
related types (meaning neither one is a superclass of the other).  In JDK
5.0 the ternairy operator will compile correctly because both types are
interpreted as their most specific common superclass - ie Window.  
However, as the error message states, there is no JDialog(Window)
constructor, so although the ternairy operator compiles, the constructor
call doesn't.

If you know, as your code suggests, that parent is either a Dialog or a
Frame, then why would you use pass it to the constructor as Window?  
Instead just have two overloaded constructors.

public class Hmm extends JDialog
{
  public Hmm(Dialog parent)
  {
     super(parent);
  }

  public Hmm(Frame parent)
  {
     super(parent);
  }
}

Signature

Beware the False Authority Syndrome

Fred - 09 Jan 2006 13:08 GMT
>In my mind this should work because the JDialog constructor has
>overloads for both Dialog and Frame.

Are you sure about this?
Stefan Schulz - 09 Jan 2006 13:57 GMT
> In my mind this should work because the JDialog constructor has
> overloads for both Dialog and Frame.  (Both derive from Window.)

That is right, however the compiler will decide which constructor to
invoke at compile time, not at run time. Since it can not find a
constructor matching the most specific common supertype (Window) it
rejects the invokation.

Using two constructors seems to be the way to go.
ricky.clarkson@gmail.com - 13 Jan 2006 01:58 GMT
> Using two constructors seems to be the way to go.

Well, the constructors are already defined in JDialog, so it would be
easier to just use JDialog, rather than extend it.

JDialog dialog=new JDialog();
dialog.setStuff();
dialog.add(stuff);
dialog.setVisible(true);
ricky.clarkson@gmail.com - 13 Jan 2006 01:59 GMT
Er, that should be JDialog dialog=new JDialog(frame);


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.