File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to create an instance of a class that is outside of any package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How to create an instance of a class that is outside of any package" Watch "How to create an instance of a class that is outside of any package" New topic
Author

How to create an instance of a class that is outside of any package

Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

Suppose i have two classes with same name. One class in within a package like below:


and another class is outside any package (does not belong to any package)like


How can i create an instance of HelloWorld class that is not inside any package from within com.my.HelloWorld class (at line // Insert code here)?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

You can't, because it is impossible to import the class in the default package in the source file that contains the class that's in a package. Therefore you should not put classes in the default package.

(It used to be possible to import classes from the default package by writing import HelloWorld; but Sun removed that feature since Java 5, I think).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Abhi Deshmukh
Greenhorn

Joined: May 05, 2010
Posts: 7
You can create an instance of HelloWorld class that is not inside any package from within com.my.HelloWorld class with the help of reflections API.



Output is :-

outer


SCJP 5.0
Moguluri Ravi Kiran
Ranch Hand

Joined: Apr 16, 2010
Posts: 63

Thank you Abhi Deshmuk , i got what you coded , its really helpful..
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Abhi Deshmukh wrote:You can create an instance of HelloWorld class that is not inside any package from within com.my.HelloWorld class with the help of reflections API.


Whilst this is a good example, I don't believe that the Reflections package is part for SCJP, so just take that into account, to avoid any confusion with your studying


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Abhi,

That was very useful. I didn't know it before, and didn't even realize that reflection Api is so strong.

Warm Regards,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Prabhat Shankar
Greenhorn

Joined: Oct 08, 2009
Posts: 27
ya that is good answer .

but when I tried above program I am getting error in line no:12

Please help me.

Prabhat Shankar Consumer Court,Consumer Court India,Consumer Complaints,Complaint India, Web Value
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi shankar,

Please quote your error message.Because i try to run the code that doesn't show any error.


Cheers Munees
My Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create an instance of a class that is outside of any package