• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem Accessing Inner Classes Constants with JSP

 
James Gordon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to group my constants using inner classes.
Please refer below.



The problem I faced is that the constant CONST is accessible
in Java classes but when I tried to access it in JSP (ie: <%=A.B.C.CONST%>,
the web server throw out this sort of error:


Can't make a static reference to inner class A.B.C.CONST


Why does it works in Java classes but not in JSP?

Please comment.


Thanks in advance.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you seen the resultant servlet?? Have a look, it will surely help you alot.

Thanks.

Also see this: http://www.javaranch.com/campfire/StoryInner.jsp
[ August 29, 2005: Message edited by: Adeel Ansari ]
 
James Gordon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JSP's servlet code looks fine.

Tried making the inner classes static and this
seemed to solve my problems. The constants
is now accessible for both Java classes
and JSPs.

Thanks.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Gordon:
Tried making the inner classes static and this
seemed to solve my problems.


I hate static!

You've probably heard about static inner classes. Well, they don't deserve to be called inner classes!

A static inner class (an inner class marked as static) looks like this:


I don't like them because they don't give me that special object-to-object bond. In fact, static inner classes aren't even supposed to be called inner classes at all. Technically, they are "top-level nested classes".

A static nested class can be instantiated, but the object created doesn't share any special relationship with an outer object.

The static nested class is tied only to the outer class, not an instance of the outer class.

Outer.Inner i = new Outer.Inner();

That's why you can make an instance of the static nested class without having an instance of the outer class, just the way you can call static methods of a class without having any instances of that class. A top-level nested class is little more than another way to control namespace.

- from a campfire story

Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!