Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Problem when calling a function


  • Please log in to reply
2 replies to this topic

#1 Qistina Tajuddin

Qistina Tajuddin

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 21 August 2011 - 06:46 PM

I wrote a function Factorial (which runs fine on its own) to be used in SampleOutputPanel.

When I compile, I get this error for some reason.

C:\Java\Assignment\Factorial>javac SampleOutputPanel.java
SampleOutputPanel.java:12: cannot find symbol
symbol : constructor Factorial(int)
location: class Factorial
f = new Factorial (5);
1 error

My code is as follows.


import javax.swing.*;
import java.awt.*;

public class Factorial {

	static int currentVal;

	public static int Factorial (int n)     {
        if (n == 0) 
			return 1;
		currentVal = n * Factorial (n - 1);
		System.out.println (currentVal);
        return (currentVal);



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SampleOutputPanel extends JPanel {

	Factorial f;
	public SampleOutputPanel () {
		f = new Factorial (5);
		setPreferredSize (new Dimension (496, 120));
		setBorder (BorderFactory.createTitledBorder ("Sample Output"));
		add (new JLabel ("Output" + f));

Please help out, thanks!
  • 0

#2 Sinipull


    CC Addict

  • Validating
  • PipPipPipPipPip
  • 384 posts

Posted 22 August 2011 - 02:46 AM

you don't have a constructor in you Factorial class. So you can't call new Factorial(5).
In this case, the static way is probably the most reasonable way to calculate the factorial, so

int f = Factorial.Factorial(5);
(Java convention says, that methods are lowerCamelCase, so you should change the Factorial() method to factorial())

you cant use Factorial f, unless you actually create the instance of Factorial object.

Also this will probably not work for you, if you are still looking to create the Factorial object:
new JLabel ("Output" + f)
because creating String from the f, which was a Factorial, calls the toString() method, which you have not implemented and results in calling the Object's toString() method instead, because of the inheritance.
If you are returning the int, then you should be just fine, because primitives will be easily converted to String.
  • 0

#3 wim DC

wim DC


  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 22 August 2011 - 12:24 PM

Actually you can use Factorial f for static methods, but it's considered bad practice. The compiler will turn any static calls on an instance into the proper code anyway.
It's easily tested by doing:
public void someMethod() {
  Factorial f = null;
will not result in a nullpointer because the compiler will see it's a static method and compile Factorial.Factorial(5) instead.
(Don't write static calls on instances like I just did)
  • 0

Also tagged with one or more of these keywords: setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download