adithya narayan

Ranch Hand
+ Follow
since Jan 05, 2009
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by adithya narayan

To simplify my question, i would like to know the scope of the static reference variable over here.
Theoretically, a static variable's scope is available as long as the class is loaded i.e. as long as the class remains in JVM.

When does a class get unloaded from JVM ? Does it ever happen ?
In my example, how long will the object referenced by the static variable be there in JVM ? As long as the JVM exists ?
IS that the reason why the 'map' variable will be present with the object since, its a part of the object ?


Thanks,
Adithya
11 years ago

Darryl Burke wrote:Objects aren't ever static. References may be static.



Can you please elaborate ? How does it relate to the scope of the 'map' variable. As it can be seen that 'Map' is private to GrandParent
and we are creating an object of Child whose reference is static, hence, map shouldn't be in the scope of Child or should it be ?
Plus, 'Map' shouldn't be visible to Child as well as AbstractParent.
11 years ago
I am not able to understand a piece of code i have encountered.

Following is a similar piece of code:



What is the scope of the 'map' variable if i create a static instance of the AbstractParent via the child ?
Map is private to GrandParent (i.e. at object level) and the object of AbstractParent is static (i.e. at class level). Could there
be a possibility that 'map' variable could be eligible for garbage collection even when AbstractParent(Child) instance exists ?

Secondly, what could be the reason behind this kind of design ?

I tested the aforementioned code to have a non-static reference to the static instance of AbstractParent as in the following example:



and found that the map contains all the 20 elements. Can anyone explain the reason behind this ? I guess i am missing something very basic over here.
11 years ago
I was just thinking of making a single object of a thread i.e. a singleton so that i have made just a single Thread object in my entire application.
Its job will be same whenever i call. The purpose of doing it is simple. I don't want to make the process run in the main application thread.

I also would like to synchronize the job (work done by runnable) if the job is called from multiple locations.

I wanted to know how good this design will be and what can be the pros and cons of it ?
@Pat Farrell : i will definitely keep your suggestion in my mind, but i just wanted to understand the functionality
as we have EJB's in our project (which again is not used so often nowadays )

@Tony Docherty : nice article..i have few questions though

1) The remote object actually never comes in picture whenever remote method invocations are being done.
I mean only stubs and skeletons are stealing the show (at least till Java 5). As far as i understand ,Stubs are
implementations of the Remote interface . Not sure on skeletons though. What is the advantage or reason of
providing the client with the Remote interface's stubs ? What are skeletons ? In the link mentioned they haven't
mentioned what exactly are skeletons.

2) Since, JAVA 2 skeletons were eliminated
(referenced from : http://docs.oracle.com/javase/1.5.0/docs/guide/rmi/spec/rmi-arch2.html ).
If there are no skeletons to handle the incoming client requests, what is actually in place ?
It would be great if you could point me to a document link or something else.

3) Can a single registry running on a single different JVM bind to multiple remote objects running on their
respective JVM's ?
12 years ago
I don't get one thing in RMI. It's a bit confusing actually.

On client side, we have the business interface (Hello.class), the client code (HelloClient.class) and the remote stub (probably Hello_stub.class) and on server side we have the server code (HelloImpl.class), the business interface (Hello.class) and the skeleton .

For Java 5 onwards, we don't create stubs but still they are c=in picture i believe.

So, how does the communication happen ?

The client calls method on Hello.class which then calls Hello_stub.class for all n/w operations. The Hello_stub.class calls the skeleton which then calls Hello.class and then calls methods on HelloImpl.class ?

I am a bit confused after reading Head first EJB .It would be glad if someone clarified it.
12 years ago
Yeah. i overlooked that statement. Should have read it completely.

Matthew Brown wrote:
Constructor objects are, but that's a bit different. Creating the object will still cause the constructor to be executed for the class you're instantiating and every super class.



I was under the impression that whenever constructor runs, an object of the residing class gets created ! Thanks for clarifying this.

12 years ago

Matthew Brown wrote:A SubA object is created (you only have one new statement, so you only create one object). But this SubA object is also a SuperA object.



You mean there is only one object creation per 'new' usage ? Can you elaborate more on the latter statement where you say SubA also a SuperA object ?

Matthew Brown wrote:
Don't think of constructors as creating an object - I think that's what's causing your confusion. When a single object is created it's entirely possible that several constructors get run. It's better to think of constructors as initialising a new object. And even if a class is abstract, when it's instantiated via a subclass there might be initialisation code that needs to be run for it (e.g. instance variables declared in the superclass might need initialising).



What about java reflection. Aren't constructors used to create/construct objects using reflection ? Please correct me if i am wrong.
12 years ago
Are objects created of abstract classes. I know you can't instantiate the class directly but if the extending class needs to constructed it cannot be complete without its parent and without the construction of its object.

If the constructor of a class runs should it always result in creation of an object ? One can place a constructor inside an abstract class.

For example, in the following program will an object be created of the abstract class SuperA ?

12 years ago
Well, why would one require

thread-safe lazy initialization idiom

?
I was just going through the following link :
http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.2

Form what little i understood, the JVM is intelligent enough to avoid dead-locks when
multiple threads simultaneously try instantiating objecs of a class. Correct me if i am wrong !
12 years ago
I was just going through the following site to know more about singletons :
http://java.sun.com/developer/technicalArticles/Programming/singletons/

but i feel that its way to advanced to understand. I know basic ways of creating singletons and their uses.
I would like to know more about Singletons. Is there a good page where it explains Singletons in deep.
I have heard Enums being Singletons; would like to know about that too.


I encountered the following piece of code used for creating a Singleton:


Can anyone explain why are using a static class over here to instantiate the Singleton class ?
12 years ago
Ok. I will keep that in mind from now onwards ..but i still don't understand the behavior which is going on in my code !
12 years ago

Matthew Brown wrote:

adithya narayan wrote:The above thing compiles. I don't get how you are allowed to not specify a type of class while creating. According to the documentation:


But you are specifying a type: Class. that class itself is generic, which may be what is confusing you. But Class<? extends BaseSessionBean>, Class<EJB>, Class<String> all resolve to the same type: Class.



I agree that i am specifying the generic type for List as Class; but i am not specifying generic type for Class. Is that acceptable with the compiler ? Why ?

You mean to say :

List<? extends Animal> listAnimal = new ArrayList<? extends Animal>(); isn't acceptable but
List<Class<? extends Animal?> listClassAnimal = new ArrayList<Class<? extends Animal>>(); is ok with the compiler. My question over here is doesn't it require the generic type of class to be specifically defined ? Is it because the compiler needs information about the first generic type only (in this case the generic type of list) ? Please explain.

Matthew Brown wrote: And what compiler error are you getting? If Mediator isn't a subclass of BaseSessionBean then you won't be able to call the method because the signature doesn't match. If it is, something else is going on.



Mediator is a sub-class of BaseSessionBean.
12 years ago
I guess i am missing something over here.

I am trying to understand the reason why it is compiling. I am trying to call the method using the following way :


The above thing compiles. I don't get how you are allowed to not specify a type of class while creating. According to the documentation:


but if i call it as follows (don't even know whether this should be the way to call the method):


it doesn't compile! Nor does the following :


I couldn't see an explanation for this in the tutorial.



12 years ago
I have the following piece of code but as per my understanding it shouldn't compile. I am not able to figure it out.



Following is the Test class :



Can anyone explain why addListWilder method compiles. The logic applied for compiling addListWild and addListWilder are same i believe.

12 years ago