Write a program in java to demonstrate instance member?

Instance Member (Error)

class InstanceMember

{

                static int a;          //Class Member

                int b;      //Instance Member of individual Object

                public static void main(String ar[])

                {

                                int c=7; //Local Variable

                                System.out.println(a);

                                System.out.println(b);

                                System.out.println(c);

                }

}
Instance Member Error (Cannot Create Object).PNG

 

Output – It gives error because instance member can invoke only with object.

 

Instance Member

class InstanceMember

{

                static int a;          //Class Member

                int b;      //Instance Member of individual Object

                public static void main(String ar[])

                {

                                int c=7; //Local Variable

                                System.out.println(a);

                                InstanceMember x=new InstanceMember();

                                System.out.println(x.b);

                                System.out.println(c);

                }

}
Instance Member.PNG
, , ,

Leave a Reply