Java Constructor, Method and Static variable – Tutorial 3

1. Method Overloading.

2. Constructors.

3. Constructors Overloading.

4. Static variable and Methods.

/*

Code:

*/

public class Student {

private int id;
private String name;

private static int count;

//Constructor Overloading
public Student()
{
//id=0;
name=null;

count++;
}
public Student(int tid,String tname)
{
id=tid;
name=tname;

count++;
}

//Method Overloading
public void setData()
{
id=0;
name=null;
}

public void setData(int tid,String tname)
{
id=tid;
name=tname;
}

public void display()
{
System.out.println(“\n ID is:”+id);
System.out.println(“\n Name is:”+name);
}

//Static Method.
public static int getCount()
{
//System.out.println(“\n ID is:”+id); //Error can’t access not static Variable/Method
return count;
}
}

public class MainClass {

public static void main(String[] args) {

Student stu1=new Student();        //Calling the user defined default constructor.
stu1.setData(1,”Vinod Pillai”);

Student stu2=new Student(2,”Abc User”); //Calling Constructor with argument.
stu2.display();

int rel=Student.getCount(); //Accessing the Static method.
System.out.println(“\nThe Static result is:”+rel);
rel=0;

rel=stu2.getCount();    //Its not the correct way but this is allowed.
System.out.println(“\nThe New Static result is:”+rel);

Student stu3=new Student();
stu3.display();
}
}

 

 

http://www.youtube.com/vinodthebest

Advertisements

3 Comments

Filed under General

3 responses to “Java Constructor, Method and Static variable – Tutorial 3

  1. I appreciate your take on this informative content. It’s immersing, specific, straightforward and very unique. I agree with a lot of your ideas and thoughts. You are a brilliant writer with a lot of intelligence.

  2. Rahul

    Hello Sir,
    Can you please upload some more videos for Java inheritance or GUI programming……
    Really your videos are best for beginner and explanatory,it helped me a lot.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s