Results 1 to 5 of 5
  1. #1
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default how to enhance code from extension class file using constructor

    hello guys is there another way to improve this code from extension class using constructor?

    Java Code:
    new Main_extension(usrnm,pwd,btn);
    the code above is working but i want to access usrnm,pwd and btn
    using "new Main_extension();" not "new Main_extension(usrnm,pwd,btn);"
    is there another way to do that? thanks...

    Full Code Main.java and Main_extension.java

    Main.java
    Java Code:
    import javax.swing.*;
    public class Main extends JFrame{ 
     private JTextField usrnm;
     private JPasswordField pwd;
     private JButton btn;
      public Main(){
       usrnm = new JTextField(10);
       pwd = new JPasswordField("mypassword",10);
       btn = new JButton("Login");
       add(usrnm);
       add(pwd);
       add(btn);
     
       new Main_extension(usrnm,pwd,btn); //<-- this work find
     
       setLayout(new java.awt.FlowLayout());
       setBounds(10,10,400,300);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       setVisible(true);
     }
     public static void main(String[]args){
       new Main();
     }
    }
    Main_extension.java
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    public class Main_extension{
     public Main_extension(final JTextField usrnm,final JPasswordField pwd,JButton btn){
      btn.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent evt){
        System.out.println("UserName: "+usrnm.getText());
    	System.out.println("Password: "+pwd.getText());
       }
      });
     }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Write a default constructor.

    Some things to remember;
    The compiler will provide a default constructor if you don't supply one which sets all objects to null, booleans to false, and numbers to 0 or 0.0

    If one constructor is provided the compiler will no longer provide the above default constructor and you will be responsible for writing it if you want it.

    Also, getting a reference to the object may be helpful.

  3. #3
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Write a default constructor.

    Some things to remember;
    The compiler will provide a default constructor if you don't supply one which sets all objects to null, booleans to false, and numbers to 0 or 0.0

    If one constructor is provided the compiler will no longer provide the above default constructor and you will be responsible for writing it if you want it.

    Also, getting a reference to the object may be helpful.
    "getting a reference to the object may be helpful" by using static variable right?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    No, static variables should be avoided except for special cases. Rather than doing
    Java Code:
    new ClassName();
    Do

    Java Code:
    ClassName varName = new ClassName();

  5. #5
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    by using ClassName varName = new ClassName(); we must use some method right?

Similar Threads

  1. Replies: 3
    Last Post: 06-21-2011, 04:51 PM
  2. Help with creating an instance of a class extension
    By Inferno719 in forum New To Java
    Replies: 19
    Last Post: 05-04-2011, 03:53 AM
  3. Replies: 3
    Last Post: 04-09-2010, 04:06 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

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