sohail hussain

Ranch Hand
+ Follow
since Sep 06, 2018
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
8
Received in last 30 days
2
Total given
2
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by sohail hussain

Heinz Teo wrote:I'm just passed 1z0-808 today with 80% score.
Been prepared for around 2 months, I had read and practice using OCA Java SE 8 Study Guide by Jeanne Boyarsky and Scott Selikoff. And also Enthuware's Mock Tests.

congratulations you did it, even not attending the all mocks you had cleared the ocajp 8 that good

Mohammed Sardar. wrote:Hi All,

From the below code I can understand that class B can access the instances and methods of class A. But I'm struggling to understand how the array of Class A allows to store the class B instances.
I googled before here but could not get the exact link, so do I need understand more in depth of IS-A and HAS-A concept  to get clear understanding ? My doubt is how class A instances and class B instances are same ? How jdk allows to treat that way ? Similarly, generics also allows to store the same way ? Can some shed some light on this please ? Thanks.

you need to know array super type, subtype relationships
Example
Array of strings can be assignable to array of object type  because string is directly extends object class implicitly
And array of object type can be assigned to object class
So A is  superclass to B
Where ever superclass object is used a subclass object can be used
1 week ago

Campbell Ritchie wrote:

sohail hussain wrote:. . . Next object is created . . .

Please be specific. Which objects are created? How many? Is the make() method called once per object or twice?

two objects are created, for every new test object make method executed
2 weeks ago

Pablo Napoli wrote:Hi everyone!. Just because many of you guys are helping me for preparing for my exam I want to "contribute" with all of them in my same situation. So this is one to practice:


I don't want to write the right answer so you can solve it and then compile and check your own one.

pablo what you are trying to explain in the code
What ever the program says that it's output is
1
1
Because you have created a static variable so whenever class is loaded static fields and static block are executed in the order they are specified in the class
Next object is created and make method is called twice so output is 1 for first method call and same for next call
It is good for practice purpose
2 weeks ago


[code=java] public static void main(String[] args) {
final int varA = 2;
final int varB;
varB = 7;  //Evething ok here
switch (0) {
case varA:
System.out.println("1");
case varB: //Compile error: Case expressions must be constant expression
System.out.println("");
}
}[/code
Hello..
See compile time constant means it is determined at compile time
But you have not given any value to varB at declaration instead you have assigned a value
varB=7; this code determined at runtime, which means in future the value of varB may change, like
varB = 9; so value may change that's why it is not final variable, if you want final variable it must be initialized at the time declaration so that it's value should not change
Hope this helps.....
2 weeks ago

Campbell Ritchie wrote:Most payments requesting credit cards can also be made by debit cards or prepaid cards.

I tried but but failed to do payment by debit card
Hello everyone
I know that ocajp8 payment mode is credit card but I don't have credit card facility what should I do
Can I use my friends card or brother's card please suggest me
Thanks...

Ivana Kilibarda wrote:Hello guys,
I cleared my OCA exam yesterday with 94% score and I am pretty satisfied. I thought to share my experience in a few words

hi congratulations Ivana you did it
Please suggest me the exam  procedure and critical questions asked
3 weeks ago

     Runtime rt = new Runtime.getRuntime();

Mike parentheses are missing in the object creation
Example,...
Runtime  rt  = new Runtime(). getRuntime() ;
Because new keword always require parentheses
I hope this works..

Mike Gualeni wrote:Given Code



The right answer in this question is:  A.  2 will be included in the output
That I understand but:

My questions is, why the Locomotive constructor can access the static main method without the legal way "Locomotive.main"? When I try "this.main" I get an info:

The static method main(String) from the type Locomotive should be accessed in a static way

   

but only mention Locomotive() { main("hi"); } is not a static way?!

Means that I can access a static method without the legal way, only calling the methodname but only in the same class?

Thanks.

constructor is a non static it can access both static and non static members of class by simple name

Pablo Napoli wrote:I see there are two possible mistakes in the second example on page 312 because when I compile and run this code I see a different behavior.

public static void main(String[] args){
try{
    throw new RuntimeException();
        } catch(RuntimeException e){
System.out.println("Inside catch block from main try- before throwing a RuntimeException");
throw new RuntimeException("Throwing a RuntimeException from catch block");
        } finally{
throw new Exception();
}
}

-> I added text to make it easier to follow.hello...

First of all how the exceptions are propogated we need know, first catch block catches the exception and again there is rethrow exception is there but that exception is not handled so it may throw exception if didn't provide finally block, so logic is finally block overrule all the priviesly uncaught exception and print the only exception which is in finally block, it is checked exception so compile time error, if it is runtime error then it will compile well but throw exception at runtime,

First, if I try to compile the code as it is, I get this error:
error: unreported exception Exception; must be caught or declared to be thrown
throw new Exception();
Which is right because the program require to handle or declare checked exceptions.

So, to continue with the example making some modifications, I choose handle the Exception like this:

try{
  throw new RuntimeException();
    } catch(RuntimeException e){
System.out.println("Inside catch block from main try- before throwing a RuntimeException");
throw new RuntimeException("Throwing a RuntimeException from catch block");
    } finally{
try{
throw new Exception();
} catch(Exception e){
System.out.println("Inside catch from finally block");
}
}
}

When I compile it, the order is the follow:

Inside catch block from main try- before throwing a RuntimeException
Inside catch from finally block
Exception in thread "main" java.lang.RuntimeException: Throwing a RuntimeException from catch block
at Test.main(Test.java:7)

So, in my opinion, the book fails when it says: Since finally block throws an exception of its own on line 31, this one gets thrown. The exception from the catch block gets forgotten about.

Because finally, the exception from catch block is thrown like we can see here: Exception in thread "main" java.lang.RuntimeException: Throwing a RuntimeException from catch block
at Test.main(Test.java:7)

What I see is that the exception from catch block is thown after the exception thrown by finally block, but this is not forbidden.

in your second code your are written the try catch in finally block which is legal and thrown a checked exception so catch block handeld it so normal execution continues, means before terminating the method finally block exicuited and there is uncaught runtime exception is there so at runtime it displays the message of exception which is not handled in the catch block means rethrow exception in catch block
1 month ago

ImDinesh Sharma wrote:I only know a tiny thing about this which is this

When we create a method, it provides the option to return some data in many forms such as int, boolean, string etc.

But

Where it returns

Why and where its useful to use this RETURN statement in method

Please could you explain this with simplest and easy way

let's take example

Class c1{
Int i=5;
boolean b=calculate() ;
Public boolean claculate() {
If(i<10)
Return true;
else
Return false ;}
Public static void main(String[] args) {
c1 t=new c1() ;
}
}
1 month ago

is not possible to that because it's array declaration of student you cannot increment in declaration
You can do like this
Creat a variable count as static in class
And put increment count++ in constructor, so when ever object is created constructor is executed so count variable will incremented by this you can count the number of object created
May it is firstname, last name which you mentioned in object creation as argument to the constructor
Firstname++, lastname++ put in constructor so it will increment the variable
1 month ago

Minesh Purohit wrote:i cleared OCAJP 8 with 80%.after 4 day i have interview in one of best company but i'm not aware that how we can make resume best and highlight our certification ?
please help me. thank you so much in advance

congratulations minesh write study plans in Wall of frame
1 month ago

Neha Agnihotri wrote:Please consider following code :

Initially I thought the answer would be compiler error as the variable was referenced before it was declared but it prints 5 as variable y is a static variable.
Now consider following code where static initialiser block is deliberately placed on top :

Now the output is 10.
Can anyone please explain me why it prints 10 instead of 5 as the static initialiser would be called after the static variable was declared and initialised.

. Neha
When the class is loaded into memory static fields and static initializes are excuited in the order they are specified,, so here first field is initialized as it is first placed and then static initializer, so y=5;
And then when object is created instance field is initialized to value which is assigned to x=y;
So it contains value 5
In second code first static block is excuited as it is placed first, assignment to static field is OK before declaration in static block,
Again static field is initialized to 10 so it contains value 10 therfore 10 is printed
Hope it clears
I suggest you to read about declaration before reading rule in initializes expression