You are on page 1of 1

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);
Note that an instance of a class can access both instance
methods and class methods but a class can only access
t4.setText("" + maxno); class methods.
Ex. String is a class. If I create an instance of String as
System.out.println(t.c); String s.
try6.c=try6.c+2;
There is a static method in String class namely ‘valueOf’.
t.b=24; Now, I can do String.valueOf as well as s.valueOf.
System.out.println(t.b); There is an instance method namely ‘length()’. I can access
t.b=t.b+2; 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