The Artima Developer Community
Sponsored Link

Java Answers Forum
Import Anomaly

6 replies on 1 page. Most recent reply: Nov 25, 2004 11:34 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Steve Schooler

Posts: 17
Nickname: zugzwang
Registered: Aug, 2004

Import Anomaly Posted: Nov 22, 2004 2:52 PM
Reply to this message Reply
Advertisement
/*  
    As is, program compiles okay.  When line (1) commented out, compile error
    (as I would expect):
    
        ImportAnomaly.java:41: cannot resolve symbol
        symbol  : class URL
        location: class ImportAnomaly
                URL url = ia.getClass().getResource("Face0.gif");      // (3)
                ^                        
    
    When lines (1), (3), and (4) are commented out, the compile error goes away.
    
    HOWEVER, with these lines commented out, line (2) is STILL creating an
    (intermediate) object of class URL, and feeding it to the ImageIcon class'
    constructor as an argument.  Why doesn't line (2) REQUIRE line (1)?  How
    does the ImageIcon constructor know that the argument is of class
    java.net.URL when, with line (1) commented out, that class is "alien" to 
    the program?
*/
 
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.net.URL;                                                   // (1)
 
public class ImportAnomaly {
    public static void main(String[] args) {
        ImportAnomaly ia = new ImportAnomaly();
        Icon icon1 = 
                new ImageIcon(ia.getClass().getResource("Face0.gif")); // (2)
        URL url = ia.getClass().getResource("Face0.gif");              // (3)
        Icon icon2 = new ImageIcon(url);                               // (4)
    }
}


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Import Anomaly Posted: Nov 22, 2004 3:42 PM
Reply to this message Reply
Unless you use the class name explictly in your code you don't have to import it.

Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: Import Anomaly Posted: Nov 23, 2004 12:50 AM
Reply to this message Reply
Hi,

I am getting compile time error if i don't comment line 2 and this is the right behaviour.
I am using j2sdk1.4.2

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Import Anomaly Posted: Nov 25, 2004 7:54 AM
Reply to this message Reply
import in Java is not the same as import in C.

What does import in Java do?

It tells the compiler:
Everytime you find a class name you don't know try looking in the import list if you can find the "path" (=package + classname) of that class there.


So:
If you WRITE "URL", the compiler does not know a class called URL, it takes a look at your imports and sees it in there.
If you use import java.net.*;, then it looks at al the classes in this package.

So why does line (2) not require an import?
Well, the compiler knows that ia.getClass().getResource("Face0.gif") returns a java.net.URL.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Import Anomaly Posted: Nov 25, 2004 8:05 AM
Reply to this message Reply
@Amol

That will only happen if you comment one of these 2 lines:
import javax.swing.Icon;
import javax.swing.ImageIcon;


So I guess you did just that (no, I KNOW that you did).

In this example only the following line should be commented:

java.net.URL;


I used Java 1.1, 1.2 a.4 and now 1.5
This is ALLWAYS the same behaviour.

You don't need import as long as you don't use a class name in your code without it's package.

java.lang and some other essentional packages are allways in the import list, you don't need to import them.

Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: Import Anomaly Posted: Nov 25, 2004 8:22 PM
Reply to this message Reply
Hi Matthias,

My mistake. You are right.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Import Anomaly Posted: Nov 25, 2004 11:34 PM
Reply to this message Reply
no problem.

Flat View: This topic has 6 replies on 1 page
Topic: Urgent Help needed to remove row in JTable! Previous Topic   Next Topic Topic: [Java] acessing CDDB

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use