-5

Here's my code:

public class MyProjectPage extends AbstractPage {
    public boolean addDefaultProjectBymyProjects(String softwareName, String projectName,) {
        navigateTomyProjects();
    } 

    public void navigateTomyProjects() {
        clickElement(By.xpath(properties.getProperty("x.Myprojects.Icon")), "MyProjects");
    }
}

I am able to access method navigateTomyProjects() without an object; how is this possible?

Note: clickElement is a generic method in abstract page

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Lokesh Sanapalli
  • 1,012
  • 3
  • 18
  • 39

2 Answers2

1

navigateTomyProjects() is an instance method.
It is being called from addDefaultProjectBymyProjects(), another instance method.

These methods are not being accessed without an object; they are being accessed by this - the current instance against which these methods will be executed.

There is nothing mysterious about this code.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • So, let's say I am calling this method navigateTomyProjects from main method, then it is giving me error, is main not considered as instance method? – Lokesh Sanapalli Jan 30 '16 at 14:41
  • @LokeshS, non-static, or instance methods cannot be called from static methods. `main()` is a static method. – Sajib Acharya Jan 30 '16 at 14:44
1

Both the methods navigateTomyProjects() and addDefaultProjectBymyProjects() reside as instance methods in the same class. As a result, either of the methods can access the other method without explicitly creating an object of the class.

To be more precise, addDefaultProjectBymyProjects() calls navigateTomyProjects() internally as this.navigateTomyProjects(). Here, the this keyword refers to the current object (invoking object).

Pratanu Mandal
  • 597
  • 8
  • 23
  • So, let's say I am calling this method navigateTomyProjects from main method, then it is giving me error, main cannot add this keyword to call that method? – Lokesh Sanapalli Jan 30 '16 at 14:42
  • 1
    Irrespective of whether the main() method is in the same class or a different class, in Java, the main method is declared as `public static void main(String[] args)`. Here, as you can observe, the main() method is `static` meaning it does not belong to any object of the class but to the class itself. On the other hand, the other methods are `instance methods`, **not** `static methods`. Static methods cannot access non-static methods with creating an object of the class. – Pratanu Mandal Jan 30 '16 at 14:47
  • Can you give any reference link to read about why can't we access static methods from non-static – Lokesh Sanapalli Jan 30 '16 at 14:48
  • These are some of the links I came across on a quick Google search. 1. [https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) 2. [https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html](https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html) 3. [http://www.lysator.liu.se/java/progGuide/java/anatomy/static.html](http://www.lysator.liu.se/java/progGuide/java/anatomy/static.html) – Pratanu Mandal Jan 30 '16 at 14:55