• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

problems in subpackage and imports

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created a package 'A' with sub-package 'B' and 'C' and some more. B and C have sub-packages B1 and C1 respectively. B1 has classes b1,b2 and C1 has classes c1,c2 etc.
Only Public access modifiers are used EVERYWHERE - methods, fields etc. Can I make c1 import import B1. This "project" consists of simple print statements and no practical methods.
You may assume that the syntax and logic is correct in each class.

any advice regarding this query will be highly appreciated.


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Yes, you can do that with an import statement. Did you try it out yourself? If you did and it didn't work, then please explain what error message you got, what command you typed in to compile your classes and what the directory structure of your code looks like.
 
Marshal
Posts: 78657
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

rahul boseofindia wrote: . . . Only Public access modifiers are used EVERYWHERE - methods, fields etc. . . . You may assume that the syntax and logic is correct in each class.

Those two sentences are mutually contradictory. Public fields are incorrect logic until proven otherwise.

What have you tried? Are you receiving any error messages?

Stop think about subpackages. Stop thinking they are sub-anything. You don't have a subpackage called B or B1 (which are incorrect style for package names). You have a package called A.B.B1, so you would have to use the full package name in your import statements.

[Damn! Jesper has beaten me to it]
 
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a very similar problem. Since I was not doing any serious project, the names are all funny.

My folder structure is like this : a folder called body. This has 3 sub-folders,left,right and centre. Left has folders hand and leg.

There are other sub-folders in the other 2 folders as well. Each folder has 1 class, TestXXXX, where XXXX is name of folder (eg. hand folder has class TestHand) and the class has only 1 method, showXXXX (eg. showHand() method in TestHand class).

The code for 1 class is like this :


The codes in other classes are similar. What I am trying to do is, access method showLeg() of TestLeg class from the hand folder using a class Test, which looks like this :


When I try to compile class Test going inside the folder hand, I get the following error : package body.left.leg does not exist

However, instead of using the Test class in the hand folder, if I use it in the parent folder that contains the body folder, I am able to access all classes anywhere in the directory structure. In other words, I cannot access a sub-package from another sub-package, but I can access any sub-package from the main package.

Can you please help me with this?
 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic