Hi guys ,
i am a to programming and have to do little code as an excercise, but got little confuse about constructors and objects while compiling my programm.

forexample
i created an object of a class called AsciiImage;
in the Main class
public class AsciiReader{
public static void main(String [] args){
int height=0;
int width=0;
AsciiImage p = new AsciiImage(width, height);
}
and the Aciiimage class
public class AsciiImage{
int height;
int width;
contstructor
public AsciiImage(int height, int width){
this.width= width;
this height=height;

the only problem by compiling is this:
cannot find symbol class AsciiImage

can some body help me
thanks

Dani AI

Generated

Quick recap and extra troubleshooting beyond what the thread covered: resolved the immediate problem by fixing a filename/classname mismatch, and correctly clarified the access‑modifier and public‑class points. Other readers who hit the same compiler message should check a few additional, often-overlooked causes.

  • Make sure the compiler actually sees the source that defines the missing type. If you compiled only one source file, the referenced class may never have been compiled or placed on the classpath.
  • Verify package declarations match your directory layout. A wrong or missing package statement (or putting files in different packages) will make the compiler/runtime fail to find the type even if the file exists.
  • Watch for case sensitivity and hidden extensions. File systems differ: "AsciiImage.java" is distinct from "asciiimage.java" on Linux, and Windows Explorer can hide a trailing ".txt".
  • Check file encoding and stray characters. A BOM or nonprintable at the start of a file can confuse the compiler.
  • Clean stale build artifacts. Remove old .class files or do a clean build in your IDE to avoid mixed versions.
  • If using an IDE, refresh the project and confirm the source folders and build path include all relevant files.

Practical compile examples:

javac MyClass.java OtherClass.java

# for a package-based source tree
javac -d out src/com/example/*.java

For authoritative guidance on packaging and visibility rules, see the Java tutorials on packages and access control: Java Packages and Access Control. These pages explain the directory/layout expectations and visibility rules that commonly cause the error discussed here.

Recommended Answers

All 7 Replies

two classes in same file?

public class AsciiImage{
int height;
int width;
//contstructor
public AsciiImage(int height, int width){
this.width= width;
this height=height;

the only problem by compiling is this:
cannot find symbol class AsciiImage

can some body help me
thanks

You dont give public to the constructor.So, your constructor should look like this:

AsciiImage(int height, int width)
{
this.width= width;
this.height=height;
}

Once you declared the class as public means, all the members (variables, and constructors and methods) are public, so you don't need to declare again constructor as public

thaks guys for your help
i get it .

Woah guys. Couple of incorrect postings here:
1. If you don't declare a constructor as public, its scope will be limited to the package in which it's declared. This may, or may not. be what you intend. If you want to call it from another package it MUST be declared public.
2. Declaring a class as public does NOT mean that its members are public. Their scope will be the classes' package unless declared otherwise.

ps: You can have 2 or more classes in 1 file, but only 1 can be declared public. because the file name must match the public classes' name.
If you think I'm wrong on any of these points, please check the Java language definition or try building a test case before flaming me - I'm only trying to help!

Sorry for the slight incorrection in my post, I stand corrected. Im facing my own dilema working within a package. Thats as far as my scope in java goes at the mo. Thread: Create & start threads with loop.

Hope you fixed you issue. :$

yeah!
I thin Jamescherill is right
because i was working on my little code AsciiReader which should have the main file code. AsciiReader.java ,,,,,,, the excutable one
and the AsciiImage.java which can is called from AsciiReader.
### the main class AsciiReader generates a onbject of the class AsciiImage.
so i definitely needed a constructor in my AsciiImage class
which as james said shouldnt be declared public.
i did't declare it public and it worked.
my problem was that the name i gave my AsciiImage didnt match the name i stored on my directory:::: tht was the reason i could'nt compile it
thnks guys

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.