Categories
discuss

In Java, if a child class shadows a static parent variable with an instance child variable, which variable will inherited methods use?

This is probably a bad thing to do, as discussed in Can parent and child class in Java have same instance variable?. (What if the parent variable name is changed? Then it will not be shadowed anymore.) However, I am still curious whether variables that are differently static/nonstatic will shadow each other. On one hand I would expect they are the same variable name so would be shadowed, but on the other hand it seems like the compiler might distinguish between the two based on staticness.

Answer

From The Java Language Specification:

If an expression name consists of a single Identifier, then there
must be exactly one visible declaration denoting either a local
variable, parameter or field in scope at the point at which the the
Identifier occurs. Otherwise, a compile-time error occurs.

If the declaration declares a final field, the meaning of the name is
the value of that field. Otherwise, the meaning of the expression name
is the variable declared by the declaration.

If a method in a superclass refers to a particular field (static or otherwise) of that class, only that class’s declaration of the field will be in scope at that point; any fields (static or otherwise) of subclasses will not be in scope. Therefore, the method will always use the superclass field, even if a subclass inherits it and shadows that field.

This answer is completely rewritten based on my new understanding of the question. Below is my first answer, kept for posterity.

From The Java Language Specification:

A declaration d of a field, local variable, method parameter,
constructor parameter or exception handler parameter named n shadows
the declarations of any other fields, local variables, method
parameters, constructor parameters or exception handler parameters
named n that are in scope at the point where d occurs throughout the
scope of d.

This suggests that compilers are required to shadow parent variables, regardless of staticness.

Note that none of this is relevant to inherited methods, which always use the original variables regardless of whether a subclass shadows them. I suspect this isn’t what you meant to ask.

Categories
discuss

Android: ExpandableListActivity icons overlapping text

I’m trying to build a very simple ExpandableListActivity based on the API Demos Sample Code:

The default layout appears broken, as the expand/collapse icons overlap the list item text.

Unfortunately, even though the demo just uses the default layout, the layout doesn’t look right. It seems the text isn’t indented enough, such that the expand/collapse icons overlap the list item text, as the screenshot demonstrates.

Why is this happening and how can I fix it?

Thanks!

Answer

The problem lies in the getGenericTextView() method of the sample code:

// Set the text starting position
textView.setPadding(36, 0, 0, 0);

setPadding(...) sets the padding (intrinsic space) in pixels, meaning the result of this indenting approach will differ per device. You seem to be using an hdpi device with a relatively large horizontal screen resolution, resulting in visually too little space on the lefthand side of the TextView. For some more explanation on this issue, please read the documentation here.

That being said, you can easily overcome the problem by setting a density independent pixel (d(i)p) value, such that the visually indented space will be identical across different resolutions. You can use the TypedValue utility class for this:

int dips = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 36, getResources().getDisplayMetrics());

Alternatively you could inflate a TextView from xml, on which you can set the density independent properties at design time, in stead of doing it on the fly at runtime.

Categories
discuss

Is there a Java equivalent of Twitter’s Ostrich library?

The ostrich project from Twitter seems to be a good fit for my use case where I would like to track lot of JVM based statistics plus some custom statistics.

https://github.com/twitter/ostrich/

However, my code base is pure Java + Spring 3.0, rather than Scala, so can I use Ostrich for my case?

Answer

I have used Metrics. It is very similar to Ostrich with support for gauges, counters, meters, histograms, and timers; As well a a mechanism to monitor the health check of your services.

You can get the report over JMX, or HTTP as well as reporting backends like Ganglia and Graphite.

Categories
discuss

Change the On/Off text of a toggle button Android

I just changed the background of a ToggleButton, and now I’m looking to change the ON/OFF text that comes up with it. What is the easiest way to do this?

Answer

You can use the following to set the text from the code:

toggleButton.setText(textOff);
// Sets the text for when the button is first created.

toggleButton.setTextOff(textOff);
// Sets the text for when the button is not in the checked state.

toggleButton.setTextOn(textOn);
// Sets the text for when the button is in the checked state.

To set the text using xml, use the following:

android:textOff="The text for the button when it is not checked."
android:textOn="The text for the button when it is checked." 

This information is from here

Categories
discuss

TypeError: ‘[object HTMLInputElement]’ is not a function (evaluating ‘elem[ type ]()’) in jQuery form.submit()

Has never run into this issue? I’m getting this error in the latest release of jQuery. I tried with version 1.6.2 and there is no issue.

TypeError: '[object HTMLInputElement]' is not a function (evaluating 'elem[ type ]()')

line 3175

Has anything changed that we should be aware of?

Thanks, Jack

Answer

This usually happens if any of your input tag’s name is submit. For example,

<form id="frm">
    <input type="submit" name="submit" value="Post" />
</form>

On the above code, document.getElementById("frm").submit represents the input element. When you apply () to submit It shows this error.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..