-2

In Java some classes require instantiation by using new keyword.

If we instantiate them by using new, then only eclipse show their methods after object as suggestions. But some classes just type class name and then class name. suggestions occurs.

Where is the difference? Specifically classes i'm talking about is :

WebDriverWait - requires instantiation

ExpectedConditions - doesn't require instantiation.

svenmeier
  • 5,681
  • 17
  • 22
Ankit
  • 51
  • 10

3 Answers3

0

Like mentioned in the other posts, this functionality is governed by the static keyword. In object oriented programming, we have two types of variables/methods, namely:

  1. Class level
  2. Instance level

Class level variables/methods are prefixed by the keyword static when they are defined, whereas the instance level variables/methods are not. We use static variables/methods when we want to define things that do not differ among different instances of the class. We can create an instance of the class and use the reference to call a static method/variable as well, but as per convention this is avoided as the static variable/method belongs to the class and not a specific instance.

You can read more about the static keyword here.

Aditya Gupta
  • 633
  • 1
  • 4
  • 11
0

Actually its really simple.
Everytime when an object of class is created i.e class is instantiated, runtime system creates a copy of all variables and methods of class for that object/instance which are called as Instance Members. Now our object will only use these copy members. To access these members we use obejctname.member.
In contrast, there is something called as Class Members. Class members are defined using static keyword. This means that class will create only single copy of these members, irrespective of how many instances are created. To access these members we use classname.staticmember.

Taking a simple example:
We have class named Person. It has instance variables like name,age and instance methods like run(), sleep(). Class contains one static method salary().

public class Person {

    //Instance Members
    private String name;
    private int age;

    public void run() {

    }

    public void sleep() {

    }

    //Class Member
    public static void salary() {

    }

    public static void main(String[] args) throws IOException {
        Person person1 = new Person();
        Person person2 = new Person();

        //accessing instance members

        String person1NameInMain = person1.name;
        int person1AgeInMain = person1.age;

        person1.run();
        person2.sleep();

        Person.salary(); // accessing static member

    }
}

That's all. I hope this helped.

-1

U should go through static keyword in java.

When u define a method as static no need to create a object u can directly call as className.methodName().

public class add{
    public static class addTwoNumber(int a, int b) {
        return a+b;
    }
}

public class staticTest{
   public static void main(String args[]) {
      system.out.println(add.addTwoNumber(2,2));
   }
}
suresh
  • 167
  • 2
  • 12