Only 49 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Should a Java Bean be always put inside a package?  RSS feed

 
tuty sra
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I would like to know is there a rule which states that Java Bean should always be in a package. I had some problems working with Java Bean having default package with some jsp action tags.


<jsp:useBean id="person3" class="test.Person">
<jsp:setProperty name="person3" property="name" value="${param.name}"/>
</jsp:useBean>

this works fine, but the same class with the package name omitted(in both java code and jsp) does not work

<jsp:useBean id="person3" class="Person">
<jsp:setProperty name="person3" property="name" value="${param.name}"/>
</jsp:useBean>


I have tried both the option with class files inside WEB-INF/classes/Person.class -> when there is no package and
WEB-INF/classes/test/Person.class -> when the class is put inside a package

Need your inputs
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, all your classes, beans or not, should be in packages. This became more of a requirement starting I think in Java 1.4 where classes inside a package can no longer import classes that are not in a package. This pretty much forces you to use packages for all of your classes (which is good practice anyway) when working with outside tools and frameworks.
 
tuty sra
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
Thanks for your reply. I got it now. Is the rule is applicable only from java1.4?. How could we import a class having default access from another class in some package in prior versions?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by tuty sra:
hi guys,
Thanks for your reply. I got it now. Is the rule is applicable only from java1.4?. How could we import a class having default access from another class in some package in prior versions?


How could we import a class having default access
Default Access is different than the default package. I will guess you meant Default Package since that is more consistent with the rest of this thread.

I don't recall exactly how to use classes in the Default Package from other classes in a package, it has been a long time since I used it. I think in normal java you didn't need to import it, you could just use it. Not sure though.
 
tuty sra
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi steve,
you are right i was actually talking about the default package. I suppose what you say is correct. we would have had the ability to import the classes in default package without specifying anything.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!