Results 1 to 20 of 23
Thread: Package access for class
- 11-30-2010, 07:24 AM #1
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
Package access for class
Hi all
i have a doubt regarding class access modifier.
i have read that class have default package access, and classes are visible in other classes in same package.
i did code in two files but package same.
but second file (which access class in first file) not get compiled....
what was the reason?
file1
------
package Pack1;
public class Test1
{
int x = 1;
}
File 2
-------
package Pack1;
public class Test2
{
public static void main(String[] args)
{
Test1 objTest1 = new Test1();
System.out.println(objTest1.x);
}
}
- 11-30-2010, 09:08 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,423
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-30-2010, 09:20 AM #3
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
thanks jos, but how i compile both files together in command prompt?
one more thing...
when i removed package statement from both files, it works fine
- 11-30-2010, 09:29 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
How are you compiling them?
What's the exact command?
What directory are you in when you compile them?
- 11-30-2010, 09:38 AM #5
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
i placed both files in a directory named Pack1
then i use command prompt and went to Pack1
then javac Test1.java it ok
then javac Test2.java it gets error
but javac *.java works fine
- 11-30-2010, 09:39 AM #6
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
thanks jos
- 11-30-2010, 09:41 AM #7
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
when i compiled both using javac *.java it worked
but on running Test2 i get error
java.lang.NoClassDefFoundError: Test2
- 11-30-2010, 09:46 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,423
- Blog Entries
- 7
- Rep Power
- 17
- 11-30-2010, 09:47 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,423
- Blog Entries
- 7
- Rep Power
- 17
- 11-30-2010, 09:58 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Yep.
Thought so.
As Jos says, classpath.
ETA: When running you should be running from the root directory. That is the directory off which your packages(s) sit, so one up from the Pack1 directory. Then you would run the code as "java Pack1/Test2".
- 11-30-2010, 10:13 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,423
- Blog Entries
- 7
- Rep Power
- 17
- 11-30-2010, 10:14 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Edited To Add.
Or, if it's trains/planes/automobiles, Estimated Time of Arrival.
:)
- 11-30-2010, 10:17 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,423
- Blog Entries
- 7
- Rep Power
- 17
- 11-30-2010, 10:36 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
I used to be confused by it as well, until someone explained it to me.
So now I use it to confuse others...I like to share.
:)
- 11-30-2010, 11:43 AM #15
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
thanks jos and tool
it worked
- 11-30-2010, 11:56 AM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
"tool"?
Should I be offended...?
;)
- 11-30-2010, 12:10 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,423
- Blog Entries
- 7
- Rep Power
- 17
- 11-30-2010, 12:52 PM #18
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
heyyyy....
sorry Tools, i typed it in a hurry....
- 11-30-2010, 01:28 PM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
It's two "l"s not two "o"s...:)
- 11-30-2010, 01:36 PM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,423
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
why I get error while trying to run a class in a package
By newlifeseattle in forum New To JavaReplies: 4Last Post: 06-01-2010, 07:15 PM -
Problem to access data when a class calls another class
By ea09530 in forum New To JavaReplies: 0Last Post: 04-04-2010, 10:06 AM -
Need to know how can we find out the package name and which class to be modified.
By shiva.b in forum New To JavaReplies: 1Last Post: 03-04-2010, 04:07 PM -
Default Access (package access) confusion
By gauravrajbehl in forum New To JavaReplies: 1Last Post: 11-18-2009, 10:48 AM -
Compiling a class in a package from cmd
By Java Tip in forum Java TipReplies: 0Last Post: 12-17-2007, 10:27 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks