Adding comments to a class

Adding comments to a class


Posted in : Core Java Posted on : October 5, 2010 at 4:39 PM Comments : [ 0 ]

In this you will learn how to add comments in java program.

Adding comments to a class

Comments are very useful for developers. It helps in taking quick review and also helpful if same code needs further modification by other programmer.

There are two type of comments :

  • Single line Comment : Single line comment needs double slashes(  //  ) to comment which causes the rest of the line ignored by the compiler.

  • Multiple line Comment : For multiple line comments , we comment inside /* and */ .

For Example :

// This is single line comment.

/* This is the demonstration of multiple
line comment in java.*/ 

Example : Given below the demo of comments inside a program :


public class Student {
	/*
	 * this instance variable is visible for any child class
	 */
	public String name;

	// salary variable is visible in Student class only.
	private int marks;

	// The name variable is assigned in the constructor.
	public Student(String StudName) {
		name = StudName;
	}

	// The marks variable is assigned a value.
	public void setMarks(int studMarks) {
		marks = studMarks;
	}

	// This method prints the student's details.
	public void printStud() {
		System.out.println("Student name  : " + name);
		System.out.println("Marks :" + marks);
	}

	public static void main(String args[]) {
		Student st = new Student("Divya");
		st.setMarks(80);
		st.printStud();
	}
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac Student.java

C:\Program Files\Java\jdk1.6.0_18\bin>java Student
Student name : Divya
Marks :80

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics