package stu;

class student

	int age;
	String name;

	public student()
  	name = "Animesh";
	age = 19;
	public void display()
	System.out.println("Student name is : " + name);
	System.out.println("Age is :"+age);
import stu.student;

class student1

	int marks;
	String sub;

	public student1()
	sub = "Mathematics";
	marks =99;

	public void display()
	System.out.println("Student marks is : " + marks);
	System.out.println("subject is :"+sub);

	public static void main(String[] args)
	student s1 = new student();
	student1 s2 = new student1();

I have got these these two class files in a Folder named 'stu'.
This folder has the path "F:\java\"

The Environment Variable is already set.

The errors which I get are related to '....class not found....'.

I cannot understand how to run this program.
Could anyone tell me the correct way to do that both the files run !!!!

Well, I do know that if you do not mark the class as public (which you did not), the default visibility is package level, which means your class can only be accessed by other classes in the same package. So either mark the class public, or put the other class in the same package. Then tell me what errors you're still getting, and paste the whole error this time.

When you use package you are creating sort of folder structure from top down. Imagine that you have following structure

  • src
    • ApplicationTest
      • model
        • Student
        • Teacher
      • gui
        • StudentGUI
        • TeacherGUI
      • dbconnection
        • DatabaseMannager
        • StudentQueries
        • TeacherQueries
    • ApplicationGUI
    • MenuGUI

ApplicationTest as being placed on the top of tree structure (in this scenario) doesn't need package declaration.
However Student being placed in model folder have to have declaration as "package model;"
So based on this make correction to your application in regards of having or removing package invocation.

Now to imports. You do not need to import classes if they are on the same level in package like ApplicationTest need access to Application GUI, or StudentQueries need access to DatabaseMannager. However as soon as caller class is on different level as called class you need to use import statement in caller class know where to find called class. Therefore StudentGUI being in package gui need access in StudentQueries that are in dbconnection package have to import dbconnection.StudentQueries; I hope this help

@ BestJewSinceJC

These are the errors that I get when I run that has the main class and imports, which is in the same package.
Package name : stu
Path : F:\java\

F:\java>cd stu

F:\java\stu>javac package stu does not exist
import stu.student;
           ^ cannot access student
bad class file: .\student.class
class file contains wrong class: stu.student
Please remove or make sure it appears in the correct subdirectory of the classpa
        student s1 = new student();
2 errors


I have attached 2 jpeg images.
one shows the java files in Folder 'stu'.
and the other shows the 'stu' folder in F:\java

If any change has to be made....please tell me !!!!!


You can swim all day in the Sea of Knowledge and still come out completely dry. Most people do.

~Norman Juster

I have got these these two class files in a Folder named 'stu'.

Therefore you do not need to use package or import.
If you decide to use package then both of the classes should have the same package declaration, but do not need "import" as they know of each other as they are on same level in folder structure.