• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

package problem

 
Arnab karmakar
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Could you please make the constructor of class1 public and check.
 
Arnab karmakar
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kaise a.Zakkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rituparno Pal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic