Author Topic: A Question about Java....  (Read 1765 times)

0 Members and 1 Guest are viewing this topic.

Offline Flipside

  • əp!sd!l£
  • 212
A Question about Java....
Ok, Im pretty comfortable with basic Java, but there's one thing that confuses me...

Why does the 'This' claimer for variables exist? People really should not be encouraged, in my opinion, to use the same name for variables as you are for fields.

My tutor says that some variable names are too obvious not to use, but I simply tag the name of the Method onto it, so age becomes, for example isSeniorAge if the method is called isSenior, etc.

I'm still trying to figure out the relevance of this command, it just strikes me that it's GOTO, waiting to happen all over again.

 

Offline Turey

  • Installer dude
  • 211
  • The diminutive form of Turambar.
    • FreeSpace Open Installer Homepage
Re: A Question about Java....
I really only use "this" in equals or compareTo methods, mostly because I name the other variable "that".  :P

I do type it a lot though, because typing "this." into Eclipse gives me a listing of all of the methods and fields of "this", sorted by return type (if you're in a spot where it's wanting an int, it'll put the int ones first, then everything else). I make my selection, then erase the "this.".
Creator of the FreeSpace Open Installer.
"Calm. The ****. Down." -Taristin
why would an SCP error be considered as news? :wtf: *smacks Cobra*It's a feature.

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: A Question about Java....
I use "this" to make references to member functions more explicit. I also use it for variables if I'm dealing with variables for the current class instance and another class instance and they're of the same time, just to make it clearer. It does get highlighted as a keyword, so it can make it a lot easier when scanning text to figure out what's going on.

As for practical uses, there are sometimes instances where you want to pass a reference to the current class to another method. For example, in Maja, I have MajaSource and MajaEntry classes. MajaSource handles actual files on disk, while MajaEntry handles virtual entries in the current VP project. In order to actually write a VP, MajaEntry calls the MajaSource extract function and passes along the reference to the current MajaEntry instance. MajaSource uses the data from that handle to extract the data from the original file.

It's not probably not ideal object-oriented programming, but it's more efficient than passing along 6+ fields and having to update all the places that the function is used when I want to add or reorder fields.
-C

 

Offline Flipside

  • əp!sd!l£
  • 212
Re: A Question about Java....
That actually makes the purpose a bit clearer, I can see where something like that might come in useful with Polymorphism etc, where you are working with several 'layers' to the data structure, I'll admit, but the way it's being taught at Uni is as though it's for people who want to name Method variables the same as their field variables for that class, which just goes against everything I've ever learned about computer coding.

Edit: Must admit, I can see the usefullness of Java, but I don't see it becoming a favourite with me, I can understand why people avoid the monolithic approach, but Java sometimes takes things from the sublime to the ridiculous. Arraylists annoy me for a start, you have to tell the computer that there is an Arraylist, and then tell it that it is empty. I can understand why it's done, but that whole 2-stage system has caught me out on several occasions with those damn 'Null Pointer' messages.
« Last Edit: January 09, 2008, 08:52:54 am by Flipside »

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: A Question about Java....
I find them a lot better than the C++ method (Crash to desktop with only a memory address as a clue to what went wrong.) :D
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline Flipside

  • əp!sd!l£
  • 212
Re: A Question about Java....
:lol: I haven't taken on C++ yet, and it looks like we'll be doing C# later this year instead. I'm not too bothered, it depends just how effective the memory management is on C#, some reports say it's fast enough to code arcade games on, others say it drags the language down terribly, so I expect it's a matter of context...

 
Re: A Question about Java....
Edit: Must admit, I can see the usefullness of Java, but I don't see it becoming a favourite with me, I can understand why people avoid the monolithic approach, but Java sometimes takes things from the sublime to the ridiculous. Arraylists annoy me for a start, you have to tell the computer that there is an Arraylist, and then tell it that it is empty. I can understand why it's done, but that whole 2-stage system has caught me out on several occasions with those damn 'Null Pointer' messages.

It's not just Java that uses this and explicit scoping is a good habit to get into when your programs start to become huge. Also, you're the first person I've heard to complain about the INconvenience of ArrayLists. :p

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: A Question about Java....
Yeah. I'm a little puzzled by that one too. What's causing you problems Flip?
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline Flipside

  • əp!sd!l£
  • 212
Re: A Question about Java....
Hehe, they aren't inconvenient, it's more about the fact that I'd just spent ages looking for why I was getting a Null Pointer on an ArrayList I'd defined, and it turned out I'd declared it, but not defined it as a new ArrayList, my own fault in general, but bloody annoying way to waste 3 hours.

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: A Question about Java....
:D

One reason I get so annoyed looking at the FS2 code is the fact that it is chock full of declared but undefined variables at the start of every function. That's breaking at least three cardinal rules of good programming style right there.
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline Flipside

  • əp!sd!l£
  • 212
Re: A Question about Java....
Ewwww.. Not good, I always remember to define ints, doubles etc, I just have to get used to remembering that 'ArrayList' is NOT an Array in the 'old' sense.

To be honest, I can't believe how ridiculously busy I am with University work, the second week in January requires 4 complete assignments to be completed that were handed out at the end of September, Java being one of them, I hate feeling like I'm rushing.

  

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: A Question about Java....
How large are those assignments?
-C

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: A Question about Java....
Ewwww.. Not good, I always remember to define ints, doubles etc, I just have to get used to remembering that 'ArrayList' is NOT an Array in the 'old' sense.

What's really funny is that the very name ArrayList is bad OO - revealing implementation details in the name instead of referring to what the object is. :D Suppose in Java 1.7 they decided to change it so that it wasn't backed by an array internally? :D
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline Flipside

  • əp!sd!l£
  • 212
Re: A Question about Java....
How large are those assignments?

They aren't massive ones, mostly Statistics, Research and Investigation, Multimedia and Programming, but to be honest, sometimes fitting something like Statistics for smoking in under 1000 words is harder than not having a word limit.

The one that is killing me is the Work-Based project, I'm having to relearn ASP, Javascript etc in order to do it, and on top of anything else, I seem to have no time for anything else these days.

Ewwww.. Not good, I always remember to define ints, doubles etc, I just have to get used to remembering that 'ArrayList' is NOT an Array in the 'old' sense.

What's really funny is that the very name ArrayList is bad OO - revealing implementation details in the name instead of referring to what the object is. :D Suppose in Java 1.7 they decided to change it so that it wasn't backed by an array internally? :D

Agreed, I keep considering them as 'Arrays', which are not initialised, merely declared usually (can you imagine looping through all the arrays at the start of a program and setting them all to '0'?), that's probably where the error creeps in.