Results 1 to 4 of 4
Like Tree3Likes
  • 1 Post By kjkrum
  • 1 Post By DarrylBurke
  • 1 Post By DarrylBurke

Thread: Augustus Gloop or Twiggy?

  1. #1
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default Augustus Gloop or Twiggy?

    Is this (gen'd via Ctrl+Shift+O in Eclipse):

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.LineNumberReader;
    import java.io.OutputStream;
    import java.net.Socket;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.ScrollPaneConstants;
    import javax.swing.Timer;
    better than this:

    Java Code:
    import java.io.*;
    import java.net.*;
    import javax.swing.*;
    import java.awt.*; 
    import java.awt.event.*;

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    I wondered that for years. But recently I ran into a situation where two classes from different packages had the same name. If I were importing the entire packages, I'd have to use the FQ name everywhere, thereby defeating the purpose of import statements.
    blackbird likes this.
    Get in the habit of using standard Java naming conventions!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    Using type-import-on-demand declarations (aka star imports) can also cause a maintenance headache. Consider a contrived example:
    Java Code:
    import java.util.*;
    // various other imports
    import java.awt.List;
    // still more imports
    A maintenance programmer may note the star import of java.util and try to add a List declaration assuming the compiler will treat it as java.util.List -- which it won't, because a single-type-import declaration shadows a type-import-on-demand declaration.
    Java Language Specification -- Packages

    db
    blackbird likes this.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    Quote Originally Posted by kjkrum View Post
    I wondered that for years. But recently I ran into a situation where two classes from different packages had the same name. If I were importing the entire packages, I'd have to use the FQ name everywhere, thereby defeating the purpose of import statements.
    Not if you used only one of the two identically named classes. And if you do need to use both, you can't escape using the FQN for at least one (but I would use the FQN for both for future readability). Continuing from the last post, this is legal and unambiguously determines that an unqualified List is a java.awt.List. Note that I don't recommend the approach, just sayin'
    Java Code:
    import java.util.*;
    import java.awt.*;
    import java.awt.List;
    db
    blackbird likes this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •