4 Replies - 11889 Views - Last Post: 20 September 2008 - 07:21 AM Rate Topic: -----

#1 ibaraku  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 190
  • Joined: 12-May 07

Calling a non-static function inside a static functons

Posted 19 September 2008 - 10:45 AM

Is there a way to call a function that is non-static say
public void printSource()



inside a static function like main

public static void main(String args[])
{
printSource();
}



without changing printSource to static???
Is This A Good Question/Topic? 0
  • +

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,804
  • Joined: 16-October 07

Re: Calling a non-static function inside a static functons

Posted 19 September 2008 - 11:28 AM

Yep, comments in the code:

// Our class is named Main
public class Main {

	// this is method for instances of Main
	public void printSource() { 
		System.out.println("Static is bad.");
	}
	
	// This is a static or class method
	// in this case, the entry point for a Java program
	// This is the only method that ever needs to be static.
	public static void main (String[] args) {
		// create an instance of the class this static method
		// belongs to
		Main pgm = new Main();
		
		// now use object methods as you like.
		pgm.printSource();
	}
}



Hope this helps.
Was This Post Helpful? 1

#5 ibaraku  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 190
  • Joined: 12-May 07

Re: Calling a non-static function inside a static functons

Posted 19 September 2008 - 02:03 PM

View Postbaavgai, on 19 Sep, 2008 - 11:28 AM, said:

Yep, comments in the code:

// Our class is named Main
public class Main {

	// this is method for instances of Main
	public void printSource() { 
		System.out.println("Static is bad.");
	}
	
	// This is a static or class method
	// in this case, the entry point for a Java program
	// This is the only method that ever needs to be static.
	public static void main (String[] args) {
		// create an instance of the class this static method
		// belongs to
		Main pgm = new Main();
		
		// now use object methods as you like.
		pgm.printSource();
	}
}



Hope this helps.

Got it.
Thanks, I appreciate it...
Was This Post Helpful? 0
  • +
  • -

#6 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Calling a non-static function inside a static functons

Posted 20 September 2008 - 06:43 AM

You were spoon fed the answer, so I hope you left with an understanding of what static means. BTW static methods are not bad. There is a place for them.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,804
  • Joined: 16-October 07

Re: Calling a non-static function inside a static functons

Posted 20 September 2008 - 07:21 AM

View PostProgrammist, on 20 Sep, 2008 - 09:43 AM, said:

You were spoon fed the answer, so I hope ...


If there is a long enough path to an answer, I will happily try to illuminate it's start and hope the student finds their way from there. However, if the path is only a single step, then leading the student on becomes more an exercised of misdirection than illumination. I don't feel such an approach does anyone any good. Alternately, you can explain that single step you must offer exhaustively enough that the experience it not lost. While I prefer the guiding approach, sometimes it's simply not an option.

View PostProgrammist, on 20 Sep, 2008 - 09:43 AM, said:

static methods are not bad. There is a place for them.


True. However, if you rarely use statics your programing does not suffer. If you overuse them, it does. For someone learning the language, and more importantly the concepts of OOP, avoiding them is less confusing than not.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1