This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!

Jason Hardaway

Greenhorn
+ Follow
since Apr 11, 2011
Jason likes ...
Java
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
4
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jason Hardaway

Mihai Boisteanu wrote:
1. It is the sintax new Foo(). When you write Foo f = new Foo() you just create a new object Foo and also reference that object with this f.
2. Instance variables are any variables, without "static" field modifier, that are defined within the class body and outside any class's methods body.
The only way you can access an object is through a reference variable. A reference variable is declared to be of a specific type and that type can never be changed. Reference variables can be declared as static variables, instance variables, method parameters, or local variables.



I apologize for the confusion but I think you misunderstood me. I understand how reference variables work but I did not know if the reference variable itself was making the recursion or if it was the new Foo() part. But yourself and Matthew have cleared that up for me now. Thank you.

I suggest you start reading some books or some online documentation until you fully understand this.


I have already read Head First Java & SCJP from front to back (although I am far from a genius). However, these did not cover recursion so I am unfamiliar with that topic like I said in my original post.
At any rate, I thank you all for your help. It is greatly appreciated.
9 years ago

Matthew Brown wrote:3. Every time you have the code new Foo(), it creates a new Foo object. Which means that in your final example, it prints the result twice because you're creating two separate objects.


I understand how this works now. Thanks

Matthew Brown wrote:1/2. Yes, it's recursion. Specifically, your line Foo f = new Foo(); And the important point is that this is declaring an instance variable of the Foo class.

Which means that whenever you create a Foo, inside that it creates another Foo. Inside that it creates another Foo. And so on.


I have 2 more questions:
1) What exactly is creating the Foo object? Is it the syntax new Foo by itself or Foo f = new Foo.
2) When you say this is declaring an instance variable, I would like to know how is this so because I'm still a little confused about reference variables.
For example:
If I were to say int x = 5; I know that the value of 5 is stored into the integer and x is an instance variable.
Also, If I were to say Foo f = new Foo(); I thought that f is of type Foo and is just a reference to new Foo(). I just don't see how it is creating objects repeatedly.

Once again, I appreciate any assistance.
9 years ago
Hello all,
I was tinkering with some code and I made an interesting discovery by accident. The compiler would display a StackOverflowError if I did the following:
a) create an instance object of a class that is assigned to a reference variable [Line 5]
b) declare a constructor that does something.(In this case, it calls a method) [Line 7]
c) attempt to run that same constructor from main() [Line 19]

Here is the original code below. (I am using Eclipse IDE by the way. That is why I used a package if you were wondering.)

The console will display something like this.
Exception in thread "main" java.lang.StackOverflowError
at stackoverflow.foo.Foo.<init>(Foo.java:7)
at stackoverflow.foo.Foo.<init>(Foo.java:5)
at stackoverflow.foo.Foo.<init>(Foo.java:5)
at stackoverflow.foo.Foo.<init>(Foo.java:5)
at stackoverflow.foo.Foo.<init>(Foo.java:5)
There is basically something going on with the constructor (line 7) and the reference variable assignment (line 5).

I theorize that this error occurred because of:
a) Recursion: I googled 'stackoverflowerror java' and the top result displayed an answer over at (not surprisingly) stackoverflow.com. Most of the posters there said a Stackoverflow error usually occurs when there is some kind of recursion (i.e. a function/method calling itself repeatedly). At this point, I do not know much about recursion but I suspect that recursion may be the reason why this error occurred but I am not for certain.
b) The 'new' keyword: I know that we instantiate objects of a class with the 'new' keyword and the 'new' operator will return a reference to the object it created. Source:Java Tutorials - Creating Objects
In addition, on page 242 of Head First Java, it states

the key feature of a constructor is that it runs before the object can be assigned to a reference.


With that being said, I suspect that since I had already assigned my object to a reference variable but the constructor will run before the object can be assigned, perhaps this will create some type of recursion. However, I'm not for certain on this.

Below are some solutions that I have found that will make the code work. But, this does not answer the why.
Solution #1 - Comment out or remove the reference variable assignment.

The console will display Doing something...

Solution #2 - Assign the reference variable locally.

In this case, the console will display the result twice. Not sure why it does that, however.
Doing something...
Doing something...

Basically, there are 3 questions I am curious about:
1) Does the compiler display a StackOverflowError message in the original code because of recursion or is it something else?
2) If it is recursion, how is the code "calling itself"?
3) When I assign the reference variable locally (although, I did not use it) like in solution 2, why does the compiler display the result twice?

I know that this post is long and I appreciate any insight that you all can provide for me. Thanks everyone and have a great day.
9 years ago
After reading this topic, I do not think the OP is fully aware of how the current job market operates. Therefore, I would like to post this book for him and anyone else that may not understand how the current market works. Also, here is the author's website which has many career tips that I think can be useful for anyone whether they are on the market or not. I would also like to say that by calling the employers "a bunch of conceited, selfish jerks", the OP is casting shadows on the wall as stated in this article here. Lastly, I agree totally with what other posters have said in regards to having 30 certifications and 2 degrees. This does not automatically make one qualified for a particular job. Hope this was helpful.
9 years ago
Hello ranchers, I am trying to gain insight about the Spring Framework.

What are the necessary steps to begin developing in the Spring Framework?
Should a developer have a solid foundation in Core Java and if so, how proficient should said developer be in Core?

On a side note, when is a developer ready to begin working in Java EE in general?

Thank you all for your replies.
9 years ago

Campbell Ritchie wrote:
You can navigate to the “beginning Java” forum and see lots of examples where people write 50-line long main methods. You go through them and wonder who on earth is teaching them.


I did notice that and I imagine that some of them are being taught by the internet. Me personally, I like it old school...I use books. And of course, JavaRanch but what do I know.
9 years ago

Jeff Verdegan wrote: A potential employer isn't going to judge you based on the size of your "personal project" (that just sounds oh so wrong), nor will he read much of the code, if any.


Thanks, Jeff. That took a weight off of my shoulders. I had a bad experience in the past with some "recruiters" who said I did not have any "professional experience".

Jeff Verdegan wrote: Find something that interests you and work on it, and don't worry about the amount of code, or even if it's complete by the time you go interview. If I were interviewing you, to the extent that this came up at all, I would be more interested in why you chose the project, what architecture and design choices you made and why, what difficulties arose and how you handled them, if you used any technologies that we're using at my company, or at least something similar to them, etc.


I will definitely take all of this information into consideration.

Dennis Deems wrote:If I were reviewing someone's code portfolio, I wouldn't count lines. I would look for: a well-articulated, cohesive class hierarchy; strong encapsulation; focused methods that perform specific tasks; coding to interfaces; use of standard libraries, especially the Collections framework; and consistent formatting that adheres to one of the common Java conventions. Also, I would be interested in the author's approach to the problem at hand. How well do the class names and method signatures tell the story of the application?


Wow This is exactly what I would hope that all my projects would have. Thanks.

Brian Burress wrote:There will be many opinions on the #s here, but where I would steer the answer toward is that if the lines of code become "too many" then you are likely doing too much in that class or method and need to look at breaking things up to be smaller such that you'll improve the ability to test that it works (simplify debugging, etc) and should foster better re-use by having smaller, simpler, focused (atomic, fine-grained or granualar) logic that can be called on rather than a massive (coarse grained) set of logic.


I never thought of the code having too many lines but it does make sense that more is not necessarily better. Thank you for this.

Brian Burress wrote:Just my one cent worth -- the govt. took the other penny ;)


They wanted both of my pennies but I hid one of them away...but don't tell anyone. LOL
Anyway, thanks everyone for your awesome feedback.
9 years ago
Hello ranchers, I was curious to know how many lines of code on average would be within a small personal project.

I know that it all depends on the type of project but I am currently looking for a rough estimate if it is possible.

I ask because my goal is to develop some small personal projects to place in my portfolio when potential employers/clients would like to see my work.

Thank you all in advance for your help.
9 years ago

Jeff Verdegan wrote:
Yeah, that's pretty much it.


I understand now. Thanks.
9 years ago

Jeff Verdegan wrote:
That's not what I was talking about, and that is still a reference in a variable. I meant, for example, something like this:

Here, there is a reference to the newly created object in our bar() method, but it's not in a variable.


Thanks, I think I understand what you were saying. According to the Java Tutorials, http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
The 'new' operator returns a reference to the object it created. This reference is usually assigned to a variable of the appropriate type. However, the reference returned by the new operator does not have to be assigned to a variable.
In the code sample below, I was able to access the instance fields using the 'new' operator. Although the sample is different from the one you provided, I believe the concept is the same.

In this next sample, I was able to access the fields using the 'new' operator and also reference variables.

I knew that the fields could be accessed by using the reference variables (such as baz.b) but I originally thought that the fields could be accessed through the 'new' operator only because the instance was created (and the constructor is invoked). However, I did not know that the 'new' operator actually returned an object reference when it created the instance. I missed that part in my studying
I think this is what you were trying to point out. I am still a beginner so is this what you were trying to say?
9 years ago

Paolo Scire' Scappuzzo wrote:hi guys, i'm new to the java language, so I met some difficulties undestanding a code line:



I need to generate classes and methods that allow to use that line of code. So i've 2 questions:

1. There's a way to use query<Diary>(diaries) as a method that returns an object (istance of a class with the method execute) without the syntax object.method() or class.method()?

Moreover, i could define this method within a class where I've to use that line of code, but i would use that line of code also in other classes.

2. There's an alternative way to call that method without the standard syntax?

Thanks a lot in advance.



Hello Paolo, if I am understanding this correctly, you would like to invoke a method called execute(). Would you mind posting more code because it is difficult to gain insight of what you are trying to do with just one line of code. It also appears that you are not using Java because I've never seen the syntax object.method() or class.method() in Java unless someone knows something I have not learned yet.
9 years ago

Jeff Verdegan wrote:

Jason Hardaway wrote:
the object is automatically eligible for GC as soon as its reference variable is null



That statement is inaccurate because it implies that each object has one reference variable inherently associated with it. An object can have zero, one, or multiple references to it, and not all those references will necessarily be in variables.

An object is eligible for GC when it is no longer reachable from any "root reference."



Thank you, I didn't realize I stated it that way. Yes, you are correct when you say that all references do not have to be in variables because a class can have an instance variable that is a reference variable to another instance of the same class.
9 years ago

Jason Hardaway wrote:

Eshwar Prasad wrote:
when will the object get garbage collected. Should I reference it to NULL once it is used?


Also, you can request that the JVM run the Garbage Collector by invoking the System.gc() method but it is not guaranteed that the JVM will honor your request but I believe that it will most of the time.


I forgot to mention that it is not recommended that you run System.gc(). As Jeff stated, you are not required to help the JVM because it will make the final decision anyway.
9 years ago

Eshwar Prasad wrote:
when will the object get garbage collected. Should I reference it to NULL once it is used?



The JVM will decide when to run garbage collection depending on when it thinks it will need more heap size. However, I do not remember how the JVM decides this at the moment because I do not have my SCJP book with me. Also, you can request that the JVM run the Garbage Collector by invoking the System.gc() method but it is not guaranteed that the JVM will honor your request but I believe that it will most of the time.

If I am reading your second question correctly, as for referencing it to null, the object is automatically eligible for GC as soon as its reference variable is null. Here are more resources for you. http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html

https://coderanch.com/t/263571/java-programmer-SCJP/certification/islands-isolation-Garbage-Collection

Hope this helps.
9 years ago

Stephan van Hulst wrote:For me, to really learn a language I need to have a project to implement using that language. If I *have* to learn a specific language (for school or work) then the project is usually provided for me. If I want to learn a language for myself, I think of a project I want to do right after reading about the basics of the language. It usually involves creating some game. I feel confident about a language when I can make the basic game mechanics work, I can make a GUI for it, and I can make two players play it through a LAN.

Learning a language without being able to apply the concepts you've learned right away simply is boring, and it won't take long for me to get tired of the language.



Thank you Stephan for your help . I agree totally when you said to have a project to implement using that language. I also agree that not being able to apply the concepts will make the language boring sort of like spinning your wheels or walking in a circle but not going anywhere. Thanks.
9 years ago