Adam Chalkley

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

Recent posts by Adam Chalkley

Starting to click now, Java can have nested classes. The static keyword by convention means that a variable such as a member variable of a class belongs to the class itself and not any instance of that class i.e. the variable or method for that matter is shared among all the instances but can also be called without an instantiation of the class itself. In nested classes Static is a little different but oddly enough very similar, it means that the nested static class does not belong to any instance of its enclosing class. Another important point to make is that true non-static inner classes have a reference to the enclosing instance of the class, this means that the inner class can access all the member variables and methods of the enclosing instance. In contrast, a static nested class does not hold a reference(i.e. does not refer) to the enclosing instance of the class. The upside of this is it saves a little time and memory, if you need a nested class that doesn't refer to any of the values in the enclosing instance, then it's best to use a static nested class. To summarize, the static nested class is essentially a top-level class.

Is that correct?

@Tim That's true, I used to program a little bit in C++, nothing major but did tinker around with it for a while, the concept of references seem very similar to that of pointers, although, I know that with references(as far as I know) you can't actually get their memory address but rather their bytecode which identifies then as a unique object.

Thanks guys
1 year ago
Hey guys,

I'll preface by saying, this is my first post in 4-5 years! I took a much needed long sabbatical.

Unfortunately, this time off has obviously rusted my skills. I'm trying to get back into the swing of things. I keep on coming across the phrase "refers to", especially when dealing with inner classes(both static and non)

For example in this post by OldCurmudgeon -> https://stackoverflow.com/questions/24953370/bloch-effective-java-favor-static-classes-over-nonstatic-how-many-instances/24953859#24953859

The user says

"The Map.Entry class used by my TrieMap class does not need to refer to the object that created it so it can be made static to save the unnecessary reference."



Also in this post by Ernest Friedman-Hill   -> https://coderanch.com/t/396740/java/instantiate-static-class

Ernest says

"-- but if they don't need one, then making Wheel a static nested class rather than an inner class saves a small amount of memory, and perhaps more importantly saves the cpu time it would take to initialize that enclosing instance reference."



Further reading suggests it has something to do with an implicit reference, but what is an implicit reference? I know what a normal reference is i.e. a name of a variable that refers to an object or primitive.

Thanks
1 year ago
Hey guys,

for the past year mainly I have just been using C++,I have taken a slight detour from Java reason being this year my course focuses more on C++ rather than Java which was the first 2 years,

my question is not really a technical question but more so an opinion based one,So since this is a Java forum why do you prefer Java over other languages?

I can see some advantages such as Javas garbage collector,no out of bounds array indexing and mainly the JVM which is a huge plus.

There are some notable difference between C++ and Java,such as Java seems to create all objects on the heap where as in C++ you can declare objects on the heap or stack,operator overloading does not exist in Java,C++ makes you manage your own memory,Java does not have structs,In Java everything must be in a class so does not support functional programming.

What makes you choose Java over languages like C/C++?

There are some things I like and dislike,the thing I like about Java as I mentioned is the JVM something I don't like is it's swing and AWT gui libraries the layout manager is quite tricky and a pain to get right.

So with C++ being a faster language in general and having more options what makes Java appealing? I mean both languages syntax wise are almost identical and I would say it's actually not too hard to make the switch from one to the other.

In the future I plan on making android apps which are mainly Java based.

but if you were writing a desktop which language would you generally choose C++ or Java?

thanks
6 years ago
much appreciated Knute,it's been a while since I have touched Java
6 years ago
wow that was the problem,such a simple mistake overlooked :/


thanks Knute
6 years ago

Knute Snortum wrote:What's line 33 in the original program?  It looks like you removed the import statements which changes all the line numbers below.



line 33 was

6 years ago
also I tried a bit of debugging to see if the array list grew when I called the addUser() function and the size did increase so I have no idea why I would be getting a null pointer exception :/
6 years ago

Knute Snortum wrote:Well, the stack trace and the error message would really help...



error message : twoException in thread "main"
java.lang.NullPointerException
at Main.main(Main.java:33)



the stack trace doesn't seem to be printing maybe I've got something wrong?

6 years ago
Hi guys,

It's been a while since I've coded in Java I seem to be getting a null pointer exception but can't seem to figure out why.










any idea why this may be happening?

thanks
6 years ago
Thanks Campbell,

yes long time no speak , I have been mainly focusing on cyber security/ forensics as I have a few repeat exams in them in August,

not much programming mainly static and dynamic analysis of malware but also cryptography,and pen testing(using different frameworks such as metasploit/nmap armitage etc)

the little programming that I have been doing since is mainly with C / C++ but I feel like now having a better understanding of C++ will help me with Java aswell.
6 years ago
First off I am happy to be back I have not posted in almost a year,took some time off to focus on other aspects of the computer science field but I have been programming quite a bit just not learning much new stuff,

any who,I have a question and I couldn't find much answers online,how are GUI components such as a JButton or Jframe implemented??

are they written in assembly language,surely you cannot just create GUI components out of nothing,I am guessing assmebly is used,

and if so how is the assembly used to create them?

anybody have any links to how these components are created in code??


thanks
6 years ago
thanks guys

yeah very true I could have simplified it with one line of code thats something I need to get a hang of

and wow thanks Campbell nice implementation I will try to use that going forward


much appreciated
6 years ago
also thanks Carey nice spot
6 years ago
thanks for the replies guys that rhymes

by calling the method in.next() in the catch block it seems to have solved my problem



I know in C++ you have to do something similar because the buffer is put into an error state so you need to clear it and reset it to a normal state is it similar in java?

is that why we need to call the .next() function?

to sum it up why do we need to the the .next() function if an input mismatch occurs?

thanks
6 years ago
Hey Campbell

I simplified it a little and made a utility method to check for the correct range

but still in my try catch block I get stuck in an infinite loop the same thing occurs it keeps printing invalid 2 and choose a row

6 years ago