• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the output of this package code?  RSS feed

 
samar das
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am getting compile time error in line 6 of the code

cannot find symbol
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you get an error you should post the full error text here.

I'm going to guess a bit, but 'id' is package private, so only visible to other classes in the same package.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I copied your code and posted using code tag. From next time please always use code tag to post your code as you posted before your previous post.
Test.java:
Employee.java:

  • Here you haven't imported Employee.java in Test class so getting error. To import you have to write import p2.Employee; in Test class's import declaration.
  • Although you import p2.Employee in Test class, you will still get error here
  • Because access modifier of parameterized constructor of Employee is package-private, which is accessible with package p2 but here you are trying to access in package p1 by creating an object of Employee class, so gives error that says the constructor Employee(int, String) is not visible.
  • Although you declare constructor of Employee class as public like this
  • you will get error at 
  • Says the field Employee.id is not visible, because again the access modifier of field named id of Employee class is package-private so accessible withing package p2 only but your accessing in p1 so you have to declare it as public like this
  • Yes now you can compile and run it successfully.
  • When you don't write any access modifier while declaring classes or members then that becomes package-private or default access modifier which is accessible withing the package it is declared in.

  • Worth reading Tell The Details and Use Code Tags
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!