Why Should we override equals method in Java

In this tutorial,we will try to understand need for overriding equals method in Java.

[Solved] validation errors for already well tested third party JavaScript libraries

Problem : Sometimes Eclipse gives validation errors for already well tested third party javascript libraries while you build your code.

Solution :

Step 1 :

Right click on the project ->Properties->JavaScript->Include path.

[Solved] There are No resources that can be added or removed from the server - Tomcat

Issue : User created a dynamic web project in Eclipse and then try to deploy it on embedded Tomcat in eclipse by right clicking on server and clicking on "Add and Remove..." option.

User gets message "There are No resources that can be added or removed from the server"

How to Install SVN Plugin in Eclipse

Hi Friends,
In this tutorial we will see how to install SVN plugin in eclipse.
Prerequisite : You should have internet connection.

Can we overload or override main method in Java

Hello Friends,

In this tutorial,we will see whether we can overload or override main method in Java.This is very common interview question as well and the answer is really simple,if we don't overthink :)

How to SetUp Eclipse on Mac machine to run Java Programs

Dear Friends,

In this tutorial we will see how to setup Eclipse on Mac machine to run Java programs.So here are the steps :

Why we have private data and public getters setters to access them

It is common norm in Object oriented languages like Java to keep data which belongs to class as private and provide a public interface in the form of getter and setter method to access the data.

This wrapping up of data and methods which act on this data within single class has been named as encapsulation.In this post,let us try to understand what is the benefit we achieve out of keeping data private and with public getter ,setters to access data.



[Solved] - Cannot create a server using the selected type

Issue : User goes to New -> Server -> Apache -> Tomcat v<version> Server ,in the console of eclipse to create a server instance in eclipse,but gets message "Cannot create a server using the selected type"

as shown in below screenshot ,so user could not proceed with server definition.



    Solution : Try one or all of following 

    1) Go to Window–>Preferences–>Server–>Runtime Environments->Edit and correct path to the              tomcat directory.


2) Go to your workspace folder.You can find path to your workspace by going to File-> Switch Workspace->Other. 

3) Now Go to following directory <workspace folder>\.metadata\.plugins\org.eclipse.core.runtime\.settings

4) Delete org.eclipse.jst.server.tomcat.core.prefs

5) Delete org.eclipse.wst.server.core.prefs

6) Restart Eclipse.

Thanks for reading.

Share it people for whom you find this info useful.

What does main method do in Java? Why main method is public,static,void?

Lets try to understand, What does main method do in Java?

main method is entry point(like main gate of your house) of your stand alone java application.Having said that it means that if we have a Java project(Not a Web Project) which has many classes in it with lot of code(business logic),you need some starting point such that your code starts executing.main method is this starting point or entry gate,which then can call other classes code.
Lets take an example to check how main method gets called and how it calls other method
package com.test.main;
public class TestClass {
public static void main(String[] args) {
               System.out.println("In TestClass");
               System.out.println(new TestClass1().display());  // calling TestClass1 display method
}
}
package com.test.main;
public class TestClass1 {
        public String display(){
        return "In TestClass1";
       }
}

Let us run TestClass from command prompt or from within eclipse
command prompt :
java TestClass
Eclipse : Right click on class having main method or right click on project name ,then select Run as -> Java Applicaiton
Output :
In TestClass
In TestClass1
Additional Info :
1) If you right click on a class which does not have main method ,eclipse will not show you Run as->Java application option.
2) If you remove any of public,static,void or arguments of main method,it will not be identified by JVM, but it will be considered as overload of main method and hence will not be called by JVM.So we need to be sure that we are using correct signature of main method,if we want JVM to call this method and below are the possible signatures of main method :

Before Java 5 :

public static void main(String[] args);

After Java 5 :

public static void main(String... args);

Or

public static void main(String[] args);
Why main method is public,static,void and with String array argument?
Why main method is Public :

There are four access modifiers in Java viz. private,default,protected,public.

Class members : Instance variables + methods 

private : Class members with private access modifier can be accessed only from within that same class.

So what if main method would have been private?It could not be called by JVM which calls main from outside the class containing main method,but methods with private access are accessible only from within that class.

default : There is no such keyword as such "default",but class members without any access modifier have default access which is also called as package-private access,which means that such class members can be accessed from any class which are within same package as the class whose members we want to access.

So what if main method would have been defined with default level access,it could still not be called by JVM, as methods with default access can only be accessed from classes which are in same package.

protected : class members with protected access can be accessed from any class within same package plus from subclasses outside the package.

So what if main method would have been defined with protected access,it could still not be called by JVM,as methods with protected access can be accessed only from classes within same package and subclasses outside package.
public : class members with public access can be accessed from any class outside the class,be it classes in same package or classes in different package.
Now as JVM is sitting somewhere outside of classes of your project,so to call main method of your project,main method must have public access.
why main method is static
As main method is invoked by JVM and by that time(when JVM invokes main method) no object of class in which main method is defined exists,there should be some way to call main method.This is the reason main method has been forced by java specifications to be static.Now as static methods belong to class(and not objects),it can be called by JVM using class name.

why main method has void return type

As main method does not need to return anything to JVM,return type of main method is void.

What is the use of String array arguments in main method


main method accepts array of String.These are called command line arguments.
Consider following example :
package com.test.main;
public class TestClass {
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println("In TestClass")
}
}
So if you are executing java program from command prompt as below :
java TestClass "hello"
Output will be :
hello
in TestClass
If you want to pass more than 1 argument,you can do like
Consider following example :
package com.test.main;
public class TestClass {
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println("In TestClass")
}
}
java TestClass "hello" "Quora"
and output will be
hello
Quora
in TestClass
This was just an example.In real projects you can pass some relevant arguments which will be required to your application at start up.
For example ,you can pass name of the applicationContext file(xml) in application using Spring,which then can be used in main method to load applicationContext.
Hope this article was helpful.If you have any comments,feedback,please dont hestitate to provide same in below comments section.