This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes package problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "package problem" Watch "package problem" New topic
Author

package problem

Arnab karmakar
Ranch Hand

Joined: Oct 04, 2004
Posts: 46
Hi all,

This is the code that I have written in class1.java

package mypackage;
public class class1
{
private int data;
class1()
{
data = 100;
}
public void func()
{
System.out.println(data);
return;
}
}

After compiling the code, the class file is created inside the mypackage folder.

This is the second file

import mypackage.*;

class class2
{
public static void main(String [] args)
{
class1 ob = new class1();
ob.func();
}
}

When I try to compile this code, it shows ---"cannot access class1".
Anybody explain it plzzzz.
thankz in advance.

arnab
Vipin Das
Ranch Hand

Joined: Jul 05, 2004
Posts: 47
hi,
Could you please make the constructor of class1 public and check.
Arnab karmakar
Ranch Hand

Joined: Oct 04, 2004
Posts: 46
hi vipin,


I think u could have tried that one. And here I have tried that what u have said , but not working.

arnab
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
Hi,

add the line....

package mypackage;

to the top of the second class and keep this file in the same directory as the other file.

It will work.

Bye
Kaps


Kapil Munjal
SCJP 1.4, SCWCD 1.4
Kaise a.Zakkar
Ranch Hand

Joined: Oct 20, 2003
Posts: 47
hi my dear
try to change the accessibility modifier of the field "data" ,to a "public",then i think that things will work well
since it has the private accessibility modifier ,it is not accessable form outside the class in which it is decleard .

kaise zakkar
scjp certified


"Nothing is harder on your aurels than resting on them."<br />SCJP 1.4 89%,,SCJA 80%,SCWCD1.4 81%
Rituparno Pal
Greenhorn

Joined: Oct 10, 2004
Posts: 12
Dear kaps
The same thing is implemented before as the code given by Arnab clearly shows so. The bottomline is it is not working. Please post your replies after looking the query carefully.
regards
rituparno
Balasubramanian Hitler
Greenhorn

Joined: Feb 04, 2004
Posts: 4
Before executing set the classpath to mypackage.
For example if the classes are in c:\mypackage
set classpath=%classpath%;c:\;
Kaise a.Zakkar
Ranch Hand

Joined: Oct 20, 2003
Posts: 47
hi Retuparno
first its kaise not kaps anyway is's ok
look at the code please :
import mypackage.*;

class class2
{
public static void main(String [] args)
{
class1 ob = new class1();
ob.func();
}
}
at the bottomline in the class2 class Arnab invoks the method func,using the refernce created from the calss1 class ,and this form the syntax-point of view works well,since the constructor of the class class1 is not private and we could make instances of it .
but the worng thing with Arnab's code is that this method ,accesses the field "data" from within the class and this is true if we are inside the class class1 ,but when we are in another class the specified method will not work since the field "data " will disappear out side the method
ok Retuparno
bye
Kaise a.Zakkar
Ranch Hand

Joined: Oct 20, 2003
Posts: 47
hi Retuparno
first its kaise not kaps anyway is's ok
look at the code please :
import mypackage.*;

class class2
{
public static void main(String [] args)
{
class1 ob = new class1();
ob.func();
}
}
at the bottomline in the class2 class Arnab invoks the method func,using the refernce created from the calss1 class ,and this form the syntax-point of view works well,since the constructor of the class class1 is not private and we could make instances of it .
but the worng thing with Arnab's code is that this method ,accesses the field "data" from within the class and this is true if we are inside the class class1 ,but when we are in another class the specified method will not work since the field "data " will disappear out side the class class1
ok Retuparno
bye
Arnab karmakar
Ranch Hand

Joined: Oct 04, 2004
Posts: 46
Hi keise,

What u are telling is not working ... i think u could have atleast tried that one urself first. Plzz try those codes before tellingme.

Arnab
Vishnu Munnangi
Ranch Hand

Joined: Sep 28, 2004
Posts: 114
Hi,

I just compiled your code in two different packages. when i compiled it without making any changes I got error message saying that Class1() is not public, so i made the constructor of Class1 public, then it is working fine.

Just make the Constructor of Class1 public and the code will compile fine.
Vipin Das
Ranch Hand

Joined: Jul 05, 2004
Posts: 47
Hi,
i tried that program before i posted my opinion, and when i made the constructor public it worked.
[ October 15, 2004: Message edited by: Vipin Das ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: package problem
 
Similar Threads
Accessing component from main class.... Help!
Package & Compilation Problem
Confusion Question
Not able to create package
naming : package, inner classes