Skip to main content

Posts

Showing posts from December, 2014

When to use "this" keyword in Java?

"this" keyword plays an important role in Java to reduce the number of lines code and performance. You can observe the following important uses of "this" keyword. To refer current class instance variables. To invoke current class method. To invoke current class constructor. To return the current class instance. Example 1:  class Employee{ int employeeId; String employeeName; Employee(int employeeId,String employeeName){ this.employeeId= employeeId; this.employeeName= employeeName; } void display(){       System.out.println(employeeId+" "+employeeName); } public static void main(String args[]){                Employee e1 = new Employee (111,"John");                Employee e2 = new Employee (222,"Abraham");                e1.display();                e2.display();     } }

What do you need to do to run a class with a main() method in a package?

Example: Say, you have a class named "Pet" in a project folder "c:\myProject" and package named com.xyz.client, will you be able to compile and run it as it is? package com.xyz.client; public class Pet { public static void main(String[] args) { System.out.println("I am found in the classpath"); } } To run 􀃆 c:\myProject> java com.xyz.client.Pet