Hello, I've written a fairly simple program to print a string in a window...But my Java compiler (JGrasp) will not compile it. It says:

JDemoGraphics.java:7: cannot find symbol
symbol : method String(java.lang.String)
location: class JDemoGraphics
String companyName = String("Event Handlers Incorporated");
^
I've checked my capitalization, run a debugging program, and even added another import function.

I'm sure the answer is obvious, but I'm new enough to programming that I can't figure it out.

Thanks in advance for the help.

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

public class JDemoGraphics extends JFrame
{
	String companyName = String("Event Handlers Incorporated");

	public JDemoGraphics()
		{
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}

	public void paint(Graphics gr)
		{
			super.paint(gr);
			gr.drawString(companyName, 30, 100);
		}

	public static void main(String[] args)
	{
		JDemoGraphics frame = new JDemoGraphics();
		frame.setSize(300, 200);
		frame.setVisible(true);
	}
}

Dani AI

Generated

Nice catch from — the compile error came from trying to use the String type like a function. In Java you either write a quoted string literal or explicitly construct an object with the new operator; trying to "call" a type name as if it were a method is a syntax error. Using the literal is the normal, preferred approach; calling the String constructor (new String(...)) works but needlessly creates a duplicate object instead of reusing the interned literal.

A few Swing best-practices to make the example more robust (these go beyond the original compile issue):

  • Do your custom painting in a JPanel by overriding paintComponent(...) and call super.paintComponent(...). That keeps painting behavior predictable and compatible with Swing's double-buffering.
  • Create and show Swing UI on the Event Dispatch Thread (EDT) via SwingUtilities.invokeLater(...).
  • Add components to the frame before calling setVisible(true), and prefer pack() when layout-managed content is used.

Example pattern (keeps the above points in one place):

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.Graphics;

public class SimplePaint {
    static class DrawPanel extends JPanel {
        private final String labelText = "Sample Company";
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawString(labelText, 30, 60);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("SimplePaint");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new DrawPanel());
            frame.setSize(320, 200);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

Small troubleshooting notes: avoid unused imports, check jGrasp is using the correct JDK if compilation still fails, and prefer string literals in most cases for clarity and efficiency.

Recommended Answers

All 3 Replies

Shouldn't your String be initialized like this?

String companyName = "Event Handlers Incorporated";

Aha! That did it...The tutorial on the website that I've been running through told me to use the

String companyName = String("Event Handlers Incorporated");

Thanks!

Aha! That did it...The tutorial on the website that I've been running through told me to use the

String companyName = String("Event Handlers Incorporated");

Thanks!

You're Welcome! I think it would also work if you used (but I'm not sure)

String companyName =  new String("Event Handlers Incorporated");

but it would just be waste of typing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.