• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Package Problem

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two files in different packages i.e different folders in win xp machine

folders are E:\JProg\Access.java
E:\JProg\TestDirect\AccessModifier2.java

Now this is the simplest java program but iam unable to solve the errors the code for this two files are given below



File AccessModifier2.java



if i uncomment the above 3 lines of code i get the following errors


---------- Complier(javac) ----------
AccessModifier2.java:16: cannot find symbol
symbol : variable x
location: class JProg.Access
d.x++;
^
AccessModifier2.java:17: cannot find symbol
symbol : variable x
location: class TestDirect.AccessModifier2
x++;
^
AccessModifier2.java:18: 'void' type not allowed here
System.out.println("value"+d.testIt());
^
3 errors

Output completed (0 sec consumed)



Now let me come at the problem
Q1:Why cannot i access the Public variable x.
Q2:Why is it throwing me error that void type not allowed as testIt is returning integer var 5
Q3:Reference is accessing d.testIt() but why cannot it access d.x; as everything is public.



please help me solve this three errors.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


 
Ashutosh Chauhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Did you mean like this

package JProg.TestDirect;
import JProg.*;

public class AccessModifier2 extends JProg.Access
{
public static void main(String[] args)
{ System.out.println("Hello World!"); ..................

No it not working getting the same errors as specified earlier read the problem carefully my friend!
 
Ashutosh Chauhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IS there no man alive who can solve my damn problem
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashutosh Chauhan wrote:IS there no man alive who can solve my damn problem

Patience, patience.

I got it to work first time without any changes

[Campbell@localhost java]$ mkdir chauhan
[Campbell@localhost java]$ cd chauhan
[Campbell@localhost chauhan]$ gedit Access.java TestDirect.java& [Copy-and-paste, save]
[1] 10883
[Campbell@localhost chauhan]$ javac -d . *.java
[1]+ Done gedit Access.java TestDirect.java
[Campbell@localhost chauhan]$ java TestDirect.AccessModifier2
Hello World!
[Campbell@localhost chauhan]$

It still works without the /* */ bit.
 
Ranch Hand
Posts: 67
Mac Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need to put like this:



 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no syntax error in the original code; it runs nicely . Whether it produces a correct result I don't know; all it seems to do is print "Hello World". Failing to get a result suggests an error in the directory structure. It ought to beAnd you call it from the myDirectory directory with java TestDirect.AccessModifier2. I showed that earlier. You cannot compile AccessModifier2 if the Access class is not available, so Access must be compiled simultaneously or before AccessModifier2.
 
Ashutosh Chauhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ritchie i don't want the directory structure to get changed i wanted

Directory Inside a Directory


e:\JProg\Access.java
e:\JProg\TestDirect\AccessModifier2.java


e:\JProg contains Access.java\TestDirectory contains AccessModifier.java


//both JProg directory contains TestDirect Directory


Blue Is for Directory.
Red is for Java file.



Please note that I Can Make a reference of Access d=new Access(); inside AccessModifier2.java meaning Access.java file is Accessible now if it is Accessible then why cannot i use public variable x and i also its showing error in testIt() is void as but its not.





When you have a directory structure like this i get the problem thanks for informing that the code i have written above is correct which i already know but in a directory structure like this i only want compile this file AccessModifier2.java as Access.java file is compiling properly as its not using interclass variables.Only difficulty is compiling AccessModifier2.java in directory structure like this [Directory structure is important i need it for specific problem i have reduced the original problem to basic one so its easy for everyone to understand iam not fooling around by printing Hello World ]
Now i know how to Run the program once it gets compiled do not show me how its done by using java -cp ........

I just want to compile AccessModifier2.java as it uses instance members for JProg.Access
Campbell you have put my both files into single directory which is not i wanted i want what i specifed in the directory structure above specific no changes please!


Druv Mistry you also ignored the directory structure. i cannot access the x member from the Access.java file. I already know testIt() is returning a value and i know how to store that value, problem is that iam getting testIt() is void error,--> read the error report in first post


A very Humble thanks to all who have replied sorry for disappointing you
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your directory structure must be consistent with your package statements. Yours isn't.

And I think you are not far from violating our No 1 rule on JavaRanch: Here. I mustmake some deletions from your posting.
 
Ashutosh Chauhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The directory structure will remain the same.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your directory structure follows the package declarations, so if you want that directory structure you must change the package declarations to match.
 
Ashutosh Chauhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply i will sort out myself then
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want package JProg; in one class and package JProg.TestDirect; in the other.

By the way: those package names oughtn't to contain Capital Letters: read the Java Tutorials section.
 
Ashutosh Chauhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Waiit wait wait just read what i have to say my friend my amigo, i have already tried that after the reply from seetharaman venkatasamy,so what iam not getting is that if i can create a reference of the Access class like this Access d=new Access then i must be able to access the the public members either through inheritance or through reference as public members are available to all packages then why aren't they available iam getting errors on that got what imeant to say if Access was not accessible that is if i could'nt create a reference then it apparent that there would be a package problem now Access is accessible why aren't the members of the Access class ?




 
Ashutosh Chauhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

I have tried this but i can't get anything that can help you on any issue. I will try again and will let you know.


DKS
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arrey i got it,it was a special problem accessing outer package class from inner package, in such cases you have to do is use for compiling in your case


java -cp ../../. AccessModifier2.java



that is it see how simply it was all the people replied to you was crap anyways no issues about them have to experienced as me to solve this! hahahaha!


"Getting wrong directions for your destination when Your standing next to it"

By Hage

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic