Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to call a non static method from a static method

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to call a non static method from a static method

    Guys Please help:
    I am trying to call a non static method from the main static method but I get an error my code is:
    public class ClassRc1 extends ConfigClass {
        public DefaultSelenium browser;
    	public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException{
    	    changephoto();
    	}
    		public void login() throws FileNotFoundException, IOException, InterruptedException
    		   {
    		super.connect();
    		String url=super.config.getProperty("url");
    		String username=super.config.getProperty("username");
    		String password=super.config.getProperty("password");
     
    	    browser= new DefaultSelenium("localhost",4444 , "*firefox", "http://"+url);
    		browser.start();
    		browser.open("/");
    		browser.windowMaximize();
    		browser.type("//*[@id='email']", username);
    		browser.type("//*[@id='pass']", password);
     
    		}
    		public void changephoto() throws FileNotFoundException, IOException, InterruptedException{
    			  login();
    			  browser.click("//*[@id='u_0_f']");			
     
    		}
    }
    Could you please tell me if there is a work around.
    Thanks in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to call a non static method from a static method

    Create an instance of the class with the new statement and use the reference to that instance to call its method.
    An example from the posted code:
     browser= new DefaultSelenium("localhost",4444 , "*firefox", "http://"+url);    //  create instance
     browser.start();   // call its method
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to call a non static method from a static method

    Thanks Norm I have got it , it made confused because i am doing it in the same class, i have got it now thank you very much for your help I am very glad to be part of this amazing Forum. wish you a happy holiday!

Similar Threads

  1. Cannot call method because it is non-static
    By thegorila78 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 7th, 2013, 01:19 PM
  2. How to call a static method within a static method in the same class?
    By EDale in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2013, 04:13 AM
  3. Replies: 6
    Last Post: May 3rd, 2013, 04:25 PM
  4. update static field and call method within the class?
    By GeneralPihota in forum Object Oriented Programming
    Replies: 7
    Last Post: February 6th, 2012, 09:20 PM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM