Quazi Irfan

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

Recent posts by Quazi Irfan

Al Hobbs wrote:That's right. You can't put random bits of code in a class. It has to be in something.
The anonymous class gets its rules from the regular class for the most part.



Thank you very much for the clarification!
2 years ago

Al Hobbs wrote:They don't consider it to be a statement.

Technically, you might call it a member variable declaration.  



Makes sense. Out of curiosity I looked at the grammar and I've found why that is the case.
Source: https://docs.oracle.com/javase/specs/jls/se7/html/jls-18.html

A regular expression statement can be made out of the following rules,


But inside a class we can only reach to Expression rule only by following MemberDecl rule.



Does it mean no class accepts statements - not only anonymous class?
2 years ago
I am reading about anonymous class on Oracle java tutorial from here: https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

Under `Syntax of Anonymous Classes` section, this page says the following about anonymous class,

The anonymous class expression consists of the following:

- The new operator

- The name of an interface to implement or a class to extend. In this example, the anonymous class is implementing the interface HelloWorld.

- Parentheses that contain the arguments to a constructor, just like a normal class instance creation expression. Note: When you implement an interface, there is no constructor, so you use an empty pair of parentheses, as in this example.

- A body, which is a class declaration body. More specifically, in the body, method declarations are allowed but statements are not.



But they also provide the following example of anonymous class that contains a statement,


Can someone explain this contradiction?
2 years ago
@Ali Ro Sorry. The following would work.

You somehow need to check if additional -> is needed.

Try using for loop instead of for each and test if you are about to print the last item

if no, then print 'value ->'
if yes, then print 'value'
2 years ago
Instead of printing value + "->", try using "->" + value

Welcome to CodeRanch.
2 years ago

Stephan van Hulst wrote:
What type would o1.Inner refer to? What type would o2.Inner refer to? Are they different types? If not, why would you have different ways to refer to the same type?



I see object followed by a period as a scoping into another namespace, and they would mean the same thing. I was trying to think in terms of the language grammar.

Stephan van Hulst wrote:Another complication. Consider you have a method that accepts an object of type Inner. How would you declare the method parameter if you couldn't write Outer.Inner?



This one makes a lot of sense. Thank you!
2 years ago
I am trying to understand why I can not create a reference of an inner class using the object of the outer class.

Here is the example classes,

I know I need to have an instance of outer class to be able to instantiate inner class. So the following makes sense,

But I get an error saying 'can not resolve symbol' in the following code,



I am assuming inner class is just like any other class members(field, methods) with public access modifier, so IMO I should be able to create a reference using the outer instance. But I can not do it. What is the justification behind having a using class name instead of an instance?

Outer.Inner feels like I am referring to a static member named Inner in the Outer class.

For example, the following works,


So by looking at `Outer.Inner i` we can not tell if Inner is a static or non-static member of Outer.
2 years ago

Paul Clapham wrote:In your original example you had an array of int. Here you have an array of Integer.



Trying to understand why that would make a difference since the body of asList is the following,

4 years ago

Matthew Bendford wrote:Varargs T... is not just eyecandy to T[]



But then why the following code snippet works as expected?

4 years ago
I understand that varargs (i.e. T...) is a syntactical sugar for T[]. But Arrays.asList, that accepts varargs, behaves differently when passed T... and T[].

For example,



I am passing a int[] where T[] is expected, but the behavior is different. But why the behavior is different if T... is just a syntactical sugar for T[] ?

Thanks.
4 years ago
Thanks for the insight.
4 years ago
I can not seem to access variable declared in a static block, but I can do when it's declared as a class static variable.

For example, the following code do not work since it can not find a,



But this solution works because a is declared as class static,


I was under the impression that static block is similar to static statement. We need to use static block if we need to execute more than one expression. But clearly I am wrong.

Some insight would be appreciate. Thanks!

4 years ago
To reiterate, during compilation "HelloWorld" is put in the String pool, and during runtime, intern() is executed and results in str2 pointing to the "HelloWorld" string that was already in the string pool. Am I correct?
4 years ago
Ok, so I get false since str2 is assigned a value during runtime, and to have it in the String pool, I have to intern it.

So, I tried the following and it is working even if things appears out of order,



The output returns true, since "HelloWorld" is in the string pool. But isn't it weird that the interning operation occurs before placing "HelloWorld" to the string pool? The code still returns true when I move line 4 at the top.

Is it an indication that the compiler is going over the code over multiple passes, and "HelloWorld" is already in the pool before entering operation takes place in line 3?
4 years ago

That means that a call to a non-private instance method will always call the version provided by the actual runtime type of the object



But isn't the runtime type changed to A by the cast in line 34?
4 years ago