method argument is not accessible by name inside method - java

Is it a bug in android studio or java, don't know. I was working on an astronomy application. I have created a method to get phase of moon. It takes two argument but only one argument is accessible by name. In debugger it says argument not found but when I assign to some other variable, it gets assigned. No runtime error in execution.
Argument that is not accessible is moon_degrees but when I assign it's value to variable moon_degree it works.
Don't know what's happening
Android studio Version 2.0
windows 7 X86

This may be a known bug:
https://issuetracker.google.com/issues/37019591
You could provide your case to the bug report to help to find the cause of this.

Your method argument is accessing inside method, but if you want to see inside method variable value in Debugger, please declare this inside variable Global.

Related

How to handle cases where allocated object is never used

In some cases, the constructor call is everything needed, and I do not need any method invocation on the created object. Depending on Java-Compiler-Preferences, Eclipse gives a warning/error "The allocated object is never used" if I do not assign the created object to a variable, or a warning/error "The value of the local variable is not used" if I assign it to a variable.
I know that I can turn off the warnings/errors in the Eclipse Preferences. My question is: For which reason does Eclipse report "The allocated object is never used"? And if there are good reasons, how should I handle these cases, where no method invocation on the created object is needed?
If all your class's logic is executed in the constructor, perhaps you should move it to a static method instead of creating an instance you'll never use.
A constructor is meant to create an instance of a class to be used later. I think you might be misusing the constructor.
From Eclipse specs this,
When enabled, the compiler will issue an error or a warning when it encounters an allocated object which is not used, e.g.
if (name == null)
new IllegalArgumentException();
But it reports as a bug. Just ignore it no need.

How Console reference variable refers to some other class method

I was strucked in a place that i cannot create new instance for Console class. So i took the source code of jdk and then look into it. Then i got cleared that it was declared as "public final class Console........"... So i understood that the final class cannot be instantiated. But that is not my actual doubt. Here in the SCJP Book, i came across a line of code that tells me as Console c = System.console()
Here we cannot create new instance for console class, so creating a reference variable c. Then What is that System.console actually is?? In the book it is told as
Keep in mind that it's possible for your Java program to be running in an environment that doesn't have access to a console object, so be sure that your invocation of System.console() actually returns a valid console reference and not null.
So then i entered to look up source code for System.console().
There i happen to see System as final class and console() as static method inside that.
So how can a Console reference object refer to that console method in system class..
What is the link between these two. I thought of a polymorphic reference.
But that is not because it doesn't pass IS-A Test. So please explain me in detail about this.
Hope my question is clear. Thanks in advance. !
the final class cannot be instantiated
That is wrong , final class cannot be subclassed. It can be instantiated using the new operator if its constructor is visible . I guess Console class has private constructor. Abstract classes cannot be instantited. See the JLS 8.1.1.2:
A class can be declared final if its definition is complete and no subclasses are desired or required.
It is a compile-time error if the name of a final class appears in the extends clause (ยง8.1.4) of another class declaration; this implies that a final class cannot have any subclasses.
System.console()
Returns the unique Console object associated with the current Java virtual machine, if any.
Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.
If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method.
If no console device is available then an invocation of that method will return null.
console() is some sort of a factory method which gives you back an object of Console associated with the current JVM . The System class has knowledge of the JVM in which it is running and it is the perfect candidate to give you back the Console object.
Console c = System.console();
System.console() gives us back a reference to the Console object and you are assigning the object reference to variable c. This way you can use that reference variable c to access the properties or methods of Console object.
I believe the mistake in your understanding is here:
So i understood that the final class cannot be instantiated
Final classes cannot be extended but can be instantiated. FYI it is the abstract class than cannot be instantiated.

Declaring local variables as final without initializer and assigning in if-statement

I just made a small code change to silence a FindBugs warning which required moving some code to an anonymous inner class. In order to access some variables, I had to declare those as final. So this is the code snippet after the change:
final File[] libPath; // libPath is final but assignment takes place later
if (libraryPath != null) {
libPath = pathToFiles(libraryPath);
} else {
libPath = new File[0];
}
This compiles just fine with language set to Java 6 in current Eclipse (Version 3.7.1). However I'm quite sure this used to give an error in some previous version. Seems the compiler accepts this construct when it can determine that there will be.
My question is: is this legal in Java 6 or is it something that now works due to a side effect of Java 7 support being added to eclipse 3.7.1? We have seen such side effects with certain usage of generics that works in 3.7.1 but didn't compile in 3.7.0.
this is ok. it is called blank final
quote from wiki:
A final variable can only be initialized once, either via an
initializer or an assignment statement. It need not be initialized at
the point of declaration: this is called a "blank final" variable. A
blank final instance variable of a class must be definitely assigned
at the end of every constructor of the class in which it is declared;
similarly, a blank final static variable must be definitely assigned
in a static initializer of the class in which it is declared:
otherwise, a compile-time error occurs in both cases. [4] (Note: If
the variable is a reference, this means that the variable cannot be
re-bound to reference another object. But the object that it
references is still mutable, if it was originally mutable.)
Blank final
The blank final, which was introduced in Java 1.1, is a final variable
whose declaration lacks an initializer. [5][6] A blank final can only
be assigned once and must be unassigned when an assignment occurs. In
order to do this, a Java compiler runs a flow analysis to ensure that,
for every assignment to a blank final variable, the variable is
definitely unassigned before the assignment; otherwise a compile-time
error occurs.[7]
In general, a Java compiler will ensure that the blank final is not
used until it is assigned a value and that once assigned a value, the
now final variable cannot be reassigned another value.[8]
link: http://en.wikipedia.org/wiki/Final_%28Java%29
This was allowed and worked fine since Java 1.1 and will not get you in trouble with other compilers or IDEs.
It is standard behaviour in Java and was first formally specified in the Java Language Specification 2nd Edition.
Java Language Specification contains a whole chapter dedicated to this behaviour (Chapter 16
Definite Assignment).
This behaviour is thoroughly defined, so that I think you misinterpret something when you say that used to produce an error in previous versions.
It's fine. The variable does not have a value, and is assigned only once. It would fail if you have given it a null value initially.
I'd strongly suggest to use this code instead:
final File[] libPath = ibraryPath == null ? new File[0] : pathToFiles(libraryPath);
This does not depend on any compiler version, but is 100% supported Java with a clear meaning.
Yes, this will work and is safe to use in all java versions I've seen (1.3+).
final means that you cannot change the value of the object once it has been initialized, if you placed a null upon declaration it would've broke.

launch(ProjectTaskManagementApp.class, args); ERROR

With the following code:
launch(ProjectTaskManagementApp.class, args);
There is the error:
The method launch(Class, String[]) is undefined for the
type ProjectTaskManagementApp
Why is it doing this? Could anyone help resolve the issue.
Thanks!
It's doing that because there's no method launch that takes a Class object and an array of String as arguments in the class ProjectTaskManagementApp. The method you're thinking of may have a different name, or may take different arguments, or it may simply not be there.
Based on the fact that you're calling launch without an object reference, it looks like this call is actually inside the definition of the class ProjectTaskManagementApp. Do you mean to be calling a launch method defined in this class, or it it defined in some other class? Do you mean to be calling launch via an object?
Simply put, the class: ProjectTaskManagementApp does not contain the method with the exact parameters you are calling it with, or does not exist at all.

Simple null reference bug thats killing me (Java)

Alright so I am messing around with some code in java and I am getting a wierd error. I have my class Chaos which has a Window variable FSW, public as well. Now I have another class called Look. Chaos creates a Look and then runs the Look.Init() method. That init method runs the looks run method which tries to reference the FSW variable of its parent Chaos.
The problem is that no matter how I got about it whenever I reference -any- variable from Chaos from within Look the variable is null =/. I can call Chaos methods from the subclass Look but I cant reference variables.
Here is a link to a text hosting site, if anyone thinks it would be necessary for me to export and upload the package I guess I will but I feel like this might be just something I am not seeing that is obvious.
http://www.text-upload.com/read.php?t=1790
Your problem is your not actually referencing a variable from within Chaos, your referencing a variable from within Look.
i.e. you create a new Look() object with it's own instance of FSW which by default is initalised to null, this is never set inside Look
If you want to reference the variable in Chaos I suggest you pass the Chaos object into the Look's constructor.
So in look you would put a new field chaos, and add a constructor like so
public Look(Chaos chaos){
this.chaos = chaos
}
Inside Chaos when creating Look you would then do
new Look(this)
Inside look you could then reference chaos.FSW

Categories