Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

John Park

Greenhorn
+ Follow
since Aug 04, 2005
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by John Park

Hi All,

I tried searching for some threads for my issue, but couldn't seem to find the answer, so starting a new thread...I updated Java on Windows 7 from 1.7.0_40 to 1.7.0_45. I'm also updating from a jre package to a jdk. After installing _45, I updated %JAVA_HOME% and verified that %JAVA_HOME% is correctly set in my path. When I echo %JAVA_HOME%, I see that it's pointed to the latest version installed.

However, running the java -version command in my console still gives me the previous runtime env version. I did some research, and even snooped around the Java Control Panel, and noticed that the system runtime env was still set to the previous version, and I was also unable to edit the settings. I also noticed that in the User env section, that my latest install wasn't picked up, so I manually added it. Perhaps this could be the problem (shoddy install?), but if there's any suggestions out there on how to get java -version to display properly, I would greatly appreciate it. I will also try and re-install jdk as well.

Thanks in advance,

JP
8 years ago
Sorry, I should have stated that this was prep for exam..basically this section was stressing that anytime you have this:

class Animal{}
class Horse extends Animal{}
class TestAnimals{

public void doIt(Animal a){}
public void doIt(Horse b){}

public static void main(String [] args){

TestAnimals ta = new TestAnimals();

Animal a = new Horse(); //why set it to a horse object, and why not //an Animal object?


a.doIt(ta); //this will invoke void doIt(Animal a);

Sorry to be such a pain in the ass...

Thanks,

JP
15 years ago
sorry to sound confusing to all..if it helps any, i'm referring to pages 307 - 309 in the Java 2 Sun Certified Programmer & Developer Study Guide, copyright 2003 by Sierra/Bates.

The section on invoking overloaded methods. If you have 2 classes, Animal, and Horse, Horse extends Animal...

and when you use an Animal reference to a Horse object:

Animal animalRefToHorse = new Horse();

the animalRefToHorse reference passed into an overloaded method will always return the method which passes in type..

I don't know why you would do this:

Animal a = new Horse();

if you already know that the overloaded method you're going to invoke is going to return the job from the method passing in type Animal..

Why can't you just do Animal a = new Animal();

Thanks,

JP
15 years ago
Thanks for the response Garrett,

So with the 1st Code example you gave, if you did this:
class Foo{

//overloaded methods...

public static void main (String [] args)
{
A apple = new B();
a.doIt(apple);

Since both methods are overloaded the result would be apple invoking the method passing in the super-type A.

But why set apple to a new B object? I don't see any point in that..
15 years ago
Hi all,

I understand that when you instantiate sub-class object type B to a super class reference type A, and assuming that there are 2 overloaded methods, one passing in the superclass type, the other the subclass type, that the compiler will always look for the overloaded method passing in the reference type.

My question is that why would anyone declare A a = new B(); in the first place , instead of A a new A();?

Why would you set a super class reference to a subclass type?
15 years ago
okaaay...crazy example, but i think i understand a bit better...
so, with the example of the previous example above of going home from the airport:

//Assume Transport is an interface with signature goHome(), and that the Limousing, BMW, and Yugo classes are implementing Transport..

if(money > 1000)
Transport ride = new Limousine();
ride.goHome();

else if (money > 500)
Transport ride = new BMW();
ride.goHome();

else
Transport ride = new Yugo();
ride.goHome();

I hope i didn't confuse anyone....thanks,

JP
15 years ago
Hey all,

I understand what happens below:

class Foo{

public void eat()
{}
}

class Foo2 extends Foo{

public void eat()
{}
}

class TestFoos{

public static void main (String [] args){
Foo f = new Foo();
<b>Foo f1 = new Foo2();
f1.eat();
}
}
</b>

I know that at runtime, f1 will invoke Foo2's eat() method, assuming they both have same level access modifier.

But why? what is the benefit of creating a Foo2 instance to a reference to Foo?

Thanks,

JP
15 years ago
Gotcha. Thanks to all those who contributed.


JP
15 years ago
ok, now i'm a bit confused...

Ken, with what you said above, what about this example then?

class Foo
{
int height = 10;


void bar()
{
Foo f = new Foo();
doStuff(f);
}
doStuff(Foo g)
{
g.height = g.height + 1;
}

the object that f is pointing to, will also be affected, since f and g both point to the same object....am i missing something here?
15 years ago
cool, thanks for the help.

I was a bit confused with pass by values using object references, but i think i got it now..

basically whenever an object reference is passed into another object reference, they will possess the same reference to the SAME object..however, if the new object reference instantiates a new instance, they become referenced to 2 differnt objects..so whatever happens to the new reference..the reference which was orginially passed in will not be affected...?

Thanks,

JP
15 years ago
Thanks Ken,

Ok, I understand that, but let's say the doStuff method was like this:

class Foo
{
int height = 10;


void bar()
{
Foo f = new Foo();
doStuff(f);
}
doStuff(Foo g)
{
g.height = g.height + 1;
}

will the object's height value that f is referring to be 11 also?

Thanks,

JP
15 years ago
Hi,

I was glancing over Sierra/Bates Java 2 cert book and came across the following code example explaining pass-by-value semantics:

void bar()
{
Foo f = new Foo();
doStuff(f);
}

void doStuff(Foo g)
{
g = new Foo();
}


From this code:

1. are g and f both referring to the SAME object? or are they 2 separate instances?

2. if g's object is somehow modified from whatever value f passed to it, will f's object also be modified?


I'm confused, because from the code, 2 different instances are created, but since f is passed into g, i'm not sure if that means that both variables are pointing to the SAME object?

Thanks in advance for your help.

JP
15 years ago
Thanks to all that responded...javaranch is awesome
15 years ago
Thanks Fred, i'll definitely look into that..does "headfirst java" dive into java 5.0?

Also, what in your opinion is the best way to learn java?


Thanks,


JP
15 years ago
hello java collective..

Just wondering if anyone can recommend a good book for beginner/intermediate java programmers...

preferrably a book that's not too boring and lenghty (*cough cough* Deitel)and easy to understand?

Thanks a bunch.

JP
15 years ago