Concept Of Instance And Class Variables

  • Uploaded by: subhash varghese
  • 0
  • 0
  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Concept Of Instance And Class Variables as PDF for free.

More details

  • Words: 369
  • Pages: 1
Concept of Instance and Class Variables 1 public class try10 extends javax.swing.JFrame {

Class XI

/** Creates new form try10 */ public try10() { initComponents(); } int a,b,c; // instance variables static int x,y,z; // class variables or global variables private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { try10 t=new try10(); // an instance of class try10 being created here, ‘t’ try10.x=10; // u can access class variables by qualifying it with the class name try10.y=12; try10.z=14; System.out.println(" Value of x:[ try10.x] " +try10.x); System.out.println("Value of y:[try10.y] " +try10.y); System.out.println("Value of z: [try10.z]" +try10.z);



//try10.a=10; // error a class can't access an instance variable directly t.a=20; // fine as an object or instance of a class can access an instance variable t.b=30; t.c=40; System.out.println(" Value of a:[ t.a] " +t.a); System.out.println("Value of b:[t.b] " +t.b); System.out.println("Value of c: [t.c]" +t.c);



 

} private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { try10 t2=new try10(); // an instance or object of a class being created here ‘t2’ System.out.println(""); System.out.println(""); System.out.println("");



System.out.println(" Value of x:[ try10.x] " +try10.x); System.out.println("Value of y:[try10.y] " +try10.y); System.out.println("Value of z: [try10.z]" +try10.z); System.out.println(" Value of a:[ t2.a] " +t2.a); System.out.println("Value of b:[t2.b] " +t2.b); System.out.println("Value of c: [t2.c]" +t2.c); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new try10().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; // End of variables declaration }

Output: On Clicking Check1 Button: Value of x:[ try10.x] 10 Value of y:[try10.y] 12 Value of z: [try10.z]14 Value of a:[ t.a] 20 Value of b:[t.b] 30 Value of c: [t.c]40 On Clicking Check 2 Button Value of x:[ try10.x] 10 Value of y:[try10.y] 12 Value of z: [try10.z]14 Value of a:[ t2.a] 0 Value of b:[t2.b] 0 Value of c: [t2.c]0

An instance or object of a class can access both instance and class/static variables. An instance variable can be accessed only by the instance of the class. A class can’t access instance variable directly. Changes made to instance variables by an object is NOT reflected in other objects of the same class. Changes made to static variables by an object is reflected in other object of the same class.

Related Documents


More Documents from ""