Help coderanch get a
new server
by contributing to the fundraiser

Eli Wood

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

Recent posts by Eli Wood

The code below uses a public static variable instead of a public static method. I believe it shows how this is possible to do, and why I would consider it to be a bad idea.

Output:
Hello from an instance method! Object is sample.SimpleSingleton@82ba41.
Hello from an instance method! Object is sample.SimpleSingleton@82ba41.
This is going to be ugly....
Exception in thread "main" java.lang.NullPointerException
at sample.SingletonDriver.main(SingletonDriver.java:17)
Nitesh, I believe the situation you are asking for is often seen in the Singleton pattern.

In my example, SimpleSingleton has a private contructor, and an instance (the only instance of the class) is held in a static variable. An accessor method, getInstance(), allows other classes to retrieve this instance, although you could avoid using a method and just set the static variable (theInstance) to public so it could be retrieved directly if you really wanted (I wouldn't recommend that though).


output:
Hello from an instance method! Object is sample.SimpleSingleton@82ba41.
Hello from an instance method! Object is sample.SimpleSingleton@82ba41.

Deepa Sobin wrote:In that case, why does the below code fail to add a Number into this list which can hold any super type of Integer?


You use ? when you don't know what the specific type of the objects in the collection will be, but in this case you know the array either holds Integer objects or objects of some superclass of Integer.

Remember, the compiler only looks at the reference type of intList, not the object type. Two possibilities are that it could be an ArrayList<Number>, or perhaps an ArrayList<Integer>. As far as the compiler knows, it could be either one. So it only makes sense for the compiler to allow you to add elements which pass the IS-A test for the type declared in List<? super type>, since those objects would definitely also pass the IS-A test for any superclass of type.

Effectively that means you can only add Integer objects, or any objects of any subclass of Integer, to that List. It just so happens that Integer has no subclasses, so only Integer objects may be added.

Deepa Sobin wrote:Hi,

Can you please help me understand why the below code compiles fine



and the below one does not?



Regards
Deepa


You can't add to collections with generic types of <? extends Something>. Otherwise you could do something like this:

Nitesh Nandwana wrote:Matthew i thought about need of HAS-A relationship is only one that is, to allow more specialist classes usage hence reduction in code else i could directly write show() in Car class.

Nitesh, it is true that both the IS-A and HAS-A relationships can reduce the amount of code we write. In my opinion, though, the main benefit relates to the principle of cohesion and the ability to write cohesive classes. You will likely find references to the principle of cohesion in your study materials; it basically means that a single class should provide only closely related functionality.

Here is a silly example I coded up. You will see that the UncohesiveCar class, in order to provide required functionality without its objects HAVING a Person, must provide methods that are not closely related to cars. It is not cohesive. Since objects from the Car class HAVE a Person passenger, that method can be pushed into the Person class, where it belongs.

Tommy, if no overriding function is provided for a parent class's public instance methods then the subclass will simply inherit them, so the following two comments are incorrect. You can see the output I have appended to my earlier post to see those methods will execute.

Tommy Delson wrote:

If you are just checking one of those two value types (and I assume you are) you can use a HashSet to reduce your memory footprint while still providing fast lookups and comparisons like HashMap. If memory isn't a problem then it doesn't really matter.
12 years ago
Polymorphism in Java is simply the ability to access an object through a reference to that object's class, a reference to a superclass, or a reference to an implemented interface. It may help to remember that poly- means many and morph- means form. The object can seem to have many different forms depending on the kind of reference used to access it.

Since, for example, the program you provided uses a GameShape reference to point to a PlayerPiece object, I would say that it provides a simple example of polymorphism.

Other ways you could declare your objects?

Output of main() function:

Campbell Ritchie wrote:What is the Unicode value of that apostrophe? Is it \u0027 (') or \u2019 (’)? If you simply push the key, you usually get \u0027.


You may be able to just use the Unicode value of the troublesome value. For example, if it's \u2019 your code would look like this:
12 years ago

Nitesh Nandwana wrote:

Eli Wood wrote:



.



According to hashCode() documentation, it is invoked with object, here in above code how did you invoked and why did you so i am not getting at all.

This code creates a new Integer using the "value" of this.value, calls the toString() method to return a string representation of the Integer, then calls the String class's hashCode() function(which overrides Object's hashCode() function) on that String to create a hash code.

But that code may be too dense for just learning and is really not important for this concept. If you don't understand, I would just forget it for now.

Your first code snippet, that just returns this.value, works perfectly well.



There is no rule against having a hash code with the same value of one of your class's members (although it may not be a best practice). What, exactly, do you feel is wrong with it?

Nitesh Nandwana wrote:

Eli Wood wrote: since every two equal InstEqual objects will have equal hashcodes.



In my last example, obj1 and obj2 are equal then why their hash code values are not same as you said above.


I was referring to your first code snippet when I typed that.

When you comment out your hashCode() function, as in your second snippet, the code will use the hashCode() function from the Object class, which only fulfills the equals/hashCode contract with respect to the equals() method defined in Object.

Nitesh Nandwana wrote:I know that every object has hashcode in integer form as a key to identify that object, and according to documentation of hashCode(), it returns hashCode value of invoking object.Now question is that in my program hashCode() is return the value of instance variable value of obj1 and obj2. those are 2 and 12. Where is the hashcode of objects ?? i am feeling very irritated because i don't want value of variable if i want variable value then simply i can make a get() method and return the variable value then why to use this nonsense hashCode(),I need hashcode please help me to find it .


In your first code snippet, the hashCode() function is returning exactly what you have programmed it to return. And that snippet also fulfills the equals/hashcode contract, since every two equal InstEqual objects will have equal hashcodes. so it works. If you would still like to use more complicated-looking hashcode values, you can try this function:



Nitesh Nandwana wrote:it gives hashCode value when i dont override hashCode() but it ought to override if equals() is overridden and why it gives different hashcode of two equal objects ?.


To override the hashCode function, you must actually write the code to override it, as you did in the first example. In this case you are just inheriting the hashCode() function of Object, which does not fulfill the contract for your new equals() function. This makes sense, since the code in the Object class knows nothing about your new class or any overriding functions.

Kees Jan Koster wrote:Dear Eli,

It may be that Java is trying to allocate a massive chunk all at once and decides that is impossible without ever actually allocating it.

Have you checked the code at the points where the apps throws exeptions? What does it allocate there? Something big?

Also, you gave the JVM 1G, so it will not use (much) more than that. Java uses what you give it. It does not scan the local machine and take what is there.


Kees,

I did not mean to give the impression I thought Java was scanning the local machine.

Functionally, at this point the code is just executing a query and bringing back some records from a database. It's a little more complicated than that as it's going through some GIS middleware but there's no reason it should be trying to allocate large amounts of memory at this point. I will have to wait until I can look closer to talk more specifically about exactly what is happening. But it worked pretty well a couple of months ago and there's no way it was allocating that much memory.
15 years ago

Kees Jan Koster wrote:Dear Eli,

Well, the memory in task manager is not likely to change much, because that is probably pre-allocated to Java at JVM start. You need to look into the running application at what happens with the heap space.

You can do that with jconsole or visualvm or (my favourite) Java-monitor. Here is what a memory leak looks like when seen through Java-monitor: http://java-monitor.com/forum/showthread.php?t=150


Thank you for the recommendation on the monitoring software. I will try it as soon as I have a chance if the problem persists.

The memory parameters for this Tomcat instance specify an initial memory pool of 512 MB and maximum of 1024 MB. When I start Tomcat, I can watch its memory usage in Task Manager climb from about 80 MB initially to about 250 MB. If I play around with some of the other webapps, I can cause the memory usage to grow to ~1 GB, then shrink (my guess is this is when the GC is run) but if I use this particular webapp it will immediately throw the Java heap space error although the memory usage in Task Manager is comparatively low at that time (~300 MB).

I hope the memory profiler helps, and I will post back after I get the chance to use it, but I cannot understand how I am getting the Java heap space error when 1. The server has more memory available and 2. Task Manager shows low memory usage compared to other times.

Thanks again for the monitoring recommendations. I need something that will allow me to monitor a specific webapp, hopefully I'll find one of these tools will allow that once I have the chance to look into that.
15 years ago
One of our applications hosted on Tomcat 6.0.18 has started throwing these errors. From what I've read, this is usually solved by patching memory leaks or increasing the amount of heap space available to Tomcat on startup.

The problem is that I have already configured Tomcat to have 1 GB of memory available and this application will cause these errors to be thrown when less than 300 MB is being used. Also, the errors are thrown in a couple of seconds, which doesn't seem like it'd be enough time for this server to use up all that memory.

I have monitored Tomcat's memory usage on the server with Task Manager while these errors are happening and I can tell the memory usage doesn't change much. Any clue what might be happening?

I've included two of the errors below... I'm not sure how helpful this will be:

Jun 16, 2009 11:58:21 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet toolservlet threw exception
java.lang.OutOfMemoryError: Java heap space
at com.esri.sde.sdk.client.s$a_.o(Unknown Source)
at com.esri.sde.sdk.client.s$a_.f(Unknown Source)
at com.esri.sde.sdk.client.SeCoordinateReference.h(Unknown Source)
at com.esri.sde.sdk.client.SeCoordinateReference.f(Unknown Source)
at com.esri.sde.sdk.client.SeLayer.c(Unknown Source)
at com.esri.sde.sdk.client.SeLayer.a(Unknown Source)
at com.esri.sde.sdk.client.SeLayer.read(Unknown Source)
at com.esri.sde.sdk.client.s$a_.a(Unknown Source)
at com.esri.sde.sdk.client.s.a(Unknown Source)
at com.esri.sde.sdk.client.b.a(Unknown Source)
at com.esri.sde.sdk.client.SeLayer.getInfo(Unknown Source)
at com.esri.sde.sdk.client.SeLayer.<init>(Unknown Source)
at com.esri.sln.ss.core.search.SdeSearchEngine.searchTheme(SdeSearchEngine.java:62)
at com.esri.sln.ss.core.map.tool.SdeIdentify.useTool(SdeIdentify.java:138)
at com.esri.sln.ss.core.servlet.ToolServlet.performPost(ToolServlet.java:170)
at com.esri.sln.ss.core.servlet.BaseServlet.doPost(BaseServlet.java:138)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.ajp.AjpAprProcessor.process(AjpAprProcessor.java:419)
at org.apache.coyote.ajp.AjpAprProtocol$AjpConnectionHandler.process(AjpAprProtocol.java:378)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1509)
at java.lang.Thread.run(Unknown Source)
Jun 16, 2009 11:58:33 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet toolservlet threw exception
java.lang.OutOfMemoryError: Java heap space
15 years ago