Class member

Define Class in terms of java ?

  • Class is a definition block which define state and behaviour of object
  •  Non static data member defines the state of object.
  • Non static function member define behavior of object.

 

Member of Class
Class has two types of member

  1.  Static member.
  2. Non static member

 

Static member

  • Using static keyword you can define static data member or function member
  • Static
    member always associated with a class.
    so it refer through class.
  • When you declare member as static then only one copy created in memory
    and that is modifiable.
  •  Whenever we want only single copy we go through static member.
  • Static member are also used to share data or function across object.

 

Non static member

  •  It declare without static keyword.
  • Non static member associated with an instance. Hence we need to create
    instance in order to non static data member.
  • When ever we create instance of class, copy of class created in
    memory(only non static member)
  •  Modifying the data member of one instance will not affect to other
    instance.
How to access static member ?


package object;
import thread.mainclass;
public class Static 
{

	static int id=50;
	static String name="bhavesh";
	
	public static void main(String[] args) 
	{
		//accessing static member
		System.out.println("Hello "+Static.name);
		Sytem.out.println("your id is "+Static.id);
		
	}
	
}

output:

Hello bhavesh
your id is 50


How to access Non static member

package object;
public class A
{

  //non static member
    int k=10;

public static void main(String[] args)
  {
    A a=new A();

   //accessing non static property
   System.out.println("k value "+a.k);

  }
}

output:
k value 10

Leave a Reply