3 Replies - 26698 Views - Last Post: 15 December 2008 - 10:32 PM Rate Topic: -----

#1 rajiv49.sachdeva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-December 08

How to call a non static function from main function?

Post icon  Posted 15 December 2008 - 08:49 PM

How to call a non static function from main function?
Is This A Good Question/Topic? 0
  • +

#3 gormanator92  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 20
  • Joined: 09-October 08

Re: How to call a non static function from main function?

Posted 15 December 2008 - 09:07 PM

This is something that can be confusing before you learn the concepts of static methods. Static methods apply to the class as a whole.
For an example, if the class you are working in is called Blah and the method you want to use is doStuff(), you would do the main in this way.

public static void main (String args)
{
	Blah objVariable = new Blah();
	objVariable.doStuff();
}


If you have more questions just ask.
Was This Post Helpful? 1

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to call a non static function from main function?

Posted 15 December 2008 - 09:49 PM

View Postrajiv49.sachdeva, on 15 Dec, 2008 - 07:49 PM, said:

How to call a non static function from main function?

This question has been asked thousand of time
Newbee are usely confused between the static void main90 methid and the other ones

The easiest way is to have

class ABC {
   // constructor
   ABC() {
	 /// do all your stuff here
	}

	public static void main(string[] arg)
	{
		 new ABC();	  // that is it that is all
	}
}


Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: How to call a non static function from main function?

Posted 15 December 2008 - 10:32 PM

View Postgormanator92, on 15 Dec, 2008 - 08:07 PM, said:

This is something that can be confusing before you learn the concepts of static methods. Static methods apply to the class as a whole.
For an example, if the class you are working in is called Blah and the method you want to use is doStuff(), you would do the main in this way.

public static void main (String args)
{
	Blah objVariable = new Blah();
	objVariable.doStuff();
}


If you have more questions just ask.



Actually that is not quite right. Static in the terms of main means that it does not require an instance of an object to be created to be called. Know why main is static? Because the java virtual machine needs to be able to call main without actually creating an instance of the program class.

So this means that instead of creating an instance using Blah objVariable = new Blah(); you would instead abe able to use the class name and call doStuff() itself like Blah.doStuff();. No creation of an instance variable, using the name of the class itself you can then call the function.

The java virtual machine does this to start your program. If your program is called "MyProgram" when it starts up it calls "MyProgram.main()".

Edit: Since it calls the program main function this way, without creating an instance, anything that is called from this static context must also be static... meaning it can't be called if it was designed to be attached to an object. Since main is not attached to an object, anything main calls (directly) can also not be attached to an object.

Hope that clarifies some things for both the original poster and gormanator92.

:)

This post has been edited by Martyr2: 15 December 2008 - 10:35 PM

Was This Post Helpful? 1

Page 1 of 1