XI-CD Methods & Classes 2 //uses try6 class created previously public class try7 extends javax.swing.JFrame { /** Creates new form try7 */ public try7() { initComponents(); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { try6 t=new try6(); int x,y,z,maxno; x=Integer.parseInt(t1.getText()); y=Integer.parseInt(t2.getText()); z=Integer.parseInt(t3.getText()); maxno=t.findmax(x, y, z); t4.setText("" + maxno); System.out.println(t.c); try6.c=try6.c+2; t.b=24; System.out.println(t.b); t.b=t.b+2;
Note that an instance of a class can access both instance methods and class methods but a class can only access class methods. Ex. String is a class. If I create an instance of String as String s. There is a static method in String class namely ‘valueOf’. Now, I can do String.valueOf as well as s.valueOf. There is an instance method namely ‘length()’. I can access it as s.length() but not as String.length().
} private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { int f,ans; f=Integer.parseInt(txtFactN.getText()); ans=try6.fact(f); txtFact.setText("" + ans); System.out.println(try6.c);// c is a class variable and so can be accessed with class name try6.c=try6.c+2; System.out.println(try6.b);// error as b is an instance variable not class variable } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new try7().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JTextField t1; private javax.swing.JTextField t2; private javax.swing.JTextField t3; private javax.swing.JTextField t4; private javax.swing.JTextField txtFact; private javax.swing.JTextField txtFactN; // End of variables declaration }