Mo Jay

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

Recent posts by Mo Jay

You need to specify the object instance that you to run the thread on, do the following and it should work fine:



Cheers!!!
Lukas Smith, this code is bad: , because main() method is the last one on the stack and you don't want main() to duck an exception any further as there is nobody else down the pile to handle it.
You should handle it in main() using try/catch or have the overriding method do it.

Sorry Ankit Garg but only one object will be garbage collected and that's the object that a1 was pointing to.
With regard to the B object with string s ref in it, that object was pointed to by b (from the stack) and it is still referred to by b which I didn't see in your diagram. The following statement : a1.obj = b; all it does is just copies the reference pattern of b to a1.obj but b is still pointing to B object with s string instance variable, thus only one object is collected.

<evaluation> ? <true result> : <false result>.


To add to what Jason said: b=!b is assigning the value of !b(which is true) to b, now b becomes true. After the first ternary there is nothing to do there and second ternary will assign the value of !b to b, keep in mind that b is true from the first ternary and now in the second b will become false. So when executing the second ternary it will print hello with lower initial letter h because b is false.

A sub class can, of course, still change the value contained in the static.



This is what I am talking about, you can still access a static variable from subclass just by typing the variables name. You can also directly assigning that static variable from the parent a new value in the subclass and it works just fine. This is what I am referring to, not to go through some methods to do the modification.

can not override static method/variables . but can be accessed by sub class



Sorry, the statement above is correct only for methods and NOT for variables because you can inherit a static variable from the parent class and you can override it by changing its value. Give it a try to see.

Cheers!!!
Chandana Garlapati , to output the thread name(Jaguar) instead of "Lynx" you just need to remove Lynx from System.out....and replace it with the method that gets the thread name: Thread.currentThread().getName()

Cheers!!!
Compiler assumes that since you have provided a constructor for your class then that proves that you know what you are doing and what you really want your constructor to do for you, thus the compiler trust you and thinks that there is no reason for him to provide a default constructor.

Cheers!!

SCJP 1.4


Again Naresh Chaurasia , you are supposedly already certified therefore you shouldn't be asking basic questions like these that were part of your preparation for the SCJP exam in the first place. I Also noticed in another thread that you asked another basic question regarding checked/unchecked exception, these make me wonder how did you become SCJP and what did you use as a study material if any.
I think that this topic and hence this thread should not have been started in the first place because it is useless and leading nowhere but to an endless loop.
Everyone has his take about this topic and taking SCJP means different things to different people, nobody's point will emerge as the right/correct point.

I will not share my point or motives regarding SCJP because they mean nothing to everyone else, thus I will go on and keep investing my time doing what I think matters to me instead of wasting it on a topic that is relative with no definite answer.



Ok, you have 2 ways how to access x2 here; either keep static keyword and you can access it directly because you have copy of x2 inherited in your testpack class anyways. Or you can remove the static keyword then use an instance of testpack class to access the inherited copy of x2, check below for sample:

Either:
in pack:
in testpack:

OR:
in pack:
in testpack:



You can also use some getter method if you want but I don't think this is the main question here.

Cheers!!!
Because you didn't instantiate the outer class MyException first and you tried to instatiate the inner class, you need to do something like this:


By the way, you need to quote your source for this code and say where you got it from.

Cheers!!!

Given the following class, what is the value of Logger.logerCount after calling Logger.createInstance(-1) ?
why the output is 1 not zero??



I don't see how this program will print 1 or even zero, you are calling the method with -1 as an argument which is an exception according to your code. More over you didn't handle or catch the exception, and by the way if you can just put a System.out.println(loggerCount++) in your finally block then you would see that the value of loggerCount++ is zero indeed and not 1.

If you want the method to do what you say then you should provide some method overriding for it where you can just copy the exact getName() method from the parent A to the subclass B and at that point B will be return it in the output. Remember that the copy you have of getName() method is a copy of the A class and class B did nothing to override or change that implementation, if you just expect the method to be inherited and magically to pick instance variable of the subclass then it is not going to happen that way.

You (the programmer) need to specify what the method should and what the output should look like by providing an implementation for it.
I don't see any shadowing here, since you didn't provide any implementation of the getName() method in the B class then you will get the one from the parent class A. If you provide the implementation of that method in class B then you should get the output as A:B

Cheers!!!