• Post Reply Bookmark Topic Watch Topic
  • New Topic

calling core java program ffrom servlet

 
Shobana Sukumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to call a core java program from a servlet?
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way you call it from another core java program.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you like to load the class without instantiating from a servlet you can use

Class.forName("className");

Make sue you have a static block to call the required methods from there.
[ December 08, 2005: Message edited by: Vishnu Prakash ]
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishnu Prakash:
If you like to load the class without instantiating from a servlet you can use

Class.forName("className");

Make sue you have a static block to call the required methods from there.

[ December 08, 2005: Message edited by: Vishnu Prakash ]


But how can we call an instance method from static method without creating instance...

 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I said static block.

static blocks are executed when the class is loaded by the JVM. From within the static block you can instantiate the class and call the methods.

FYI: This is just another way of invoking a POJO from servlets.

Note: This way of invoking a POJO is useful when you have a static method that does some work and returns results.
[ December 09, 2005: Message edited by: Vishnu Prakash ]
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishnu Prakash:
I said static block.

static blocks are executed when the class is loaded by the JVM. From within the static block you can instantiate the class and call the methods.

FYI: This is just another way of invoking a POJO from servlets.


Yes, but you are not saving instantiation of class, as you mentioned in your first post...


[ December 09, 2005: Message edited by: rathi ji ]
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes, but you are not saving instantiation of class, as you mentioned in your first post...


Read the Note part of my previous post.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishnu Prakash:


Read the Note part of my previous post.


Yes, if the method is static then we don't need to instantiate a class. Should we always keep methods static to save instantiation??? or is there any drawback of this???

Thanks.

 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain a bit more about your reluctance to instantiate an object of the class you have loaded? We are using Object-Oriented programming, after all
 
Amarender Reddy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i don't think there is any disadvantage in daclaring (more) methods as static and calling them without saving the instance of class.

wht vishnu was refering to was only one of the different ways of calling the methods.

PS: Lets try to answer closely to the original query posted. I think by this time Shobana would be half confused and stopped visiting this page atleast, out of chaos.

cheers
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frank Carver:
Can you explain a bit more about your reluctance to instantiate an object of the class you have loaded? We are using Object-Oriented programming, after all


I am not in oppose of instantiating classes, after all, we can't avoid this always. But till it is not harmful, why not having static methods in model...


 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How should you determine static methods or instance methods?


If an entity have a state you will be representing the state with variables on a class and the instance of the class (object) represents an instance of that entity.

All I am trying to explain in next paragraph is "what is State". If the entity have a state use instance method else use static method. Another reason to make instance method is polymorphism.

State: A house have a propery called Occupied:boolean; House have a behaviour called getNumberOfOccupants. The property Occupied varies with every instance of House. So it is better to keep Occupied as an Instance variable.

getNumberOfOccupants first check whether house is occupied. If not occupied it returns zero, else returns numberOfOccupants:int (another Property)

Here numberOfOccupants and occupied are instance variables which are maintaining the state of an object. Since getNumberOfOccupants method relies on state of the object, this method sould be an instance method. In java this rule is contracted through language.

Polymorphism: Suppose you have a universal remote control which can be used to power off several systems like Tape player, DVD player, TV, Video Tape Player. You can off one or more systems at a time. The super class (abstract) have a method powerOff:void. The Object of type Remote gain access to all instances of System (Tape player, DVD player, TV, Video Tape Player) and invoke the method powerOff. Here powerOff is an instance method. The beauty here is Remote don't have to know the type(subclass) of system.

You couldn't have implemented this "dynamic(runtime binding) polymorphism" if you kept powerOff as static method.
[ December 12, 2005: Message edited by: jiju ka ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!