Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Max Facetime
Apr 18, 2009

Gravity Pike posted:

Shaving off six clock cycles by using two variables instead of one is going to be a gnat's fart compared to opening a TCP connection.

Yes, and while Smarmy Coworker could probably get HotSpot to spit out the assembly code that it would run and start counting instructions I wouldn't bother.

Adbot
ADBOT LOVES YOU

Smarmy Coworker
May 10, 2008

by XyloJW
yeah I usually value being readable I just totally farted on iterating all of a sudden because I'm redoing a project from the ground up and my professor told me that he grades optimization so I was scared

dereekb
Nov 7, 2010

What do you mean "error"?

rhag posted:

To make code execute from the context you can add an afterPropertiesSet method to the beans, in which you can do your stuff. Or, use a static method and call it as a factory method from the XML.

Thanks for the idea, but this still isn't quite what I'm trying to get since Maven won't run those tests. :v:


I did find some additional annotations in the Spring documentation on a second look that I think will work out by just overriding contexts through @ContextHierarchy.

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

Smarmy Coworker posted:

yeah I usually value being readable I just totally farted on iterating all of a sudden because I'm redoing a project from the ground up and my professor told me that he grades optimization so I was scared

Ah. Sadly, homework coding standards are somewhat different than professional coding standards. :(

Volguus
Mar 3, 2009

dereekb posted:

Thanks for the idea, but this still isn't quite what I'm trying to get since Maven won't run those tests. :v:


I did find some additional annotations in the Spring documentation on a second look that I think will work out by just overriding contexts through @ContextHierarchy.

I'm completely lost. So, you use maven, you put unit tests in test/java and maven won't run them? How is that possible?

dereekb
Nov 7, 2010

What do you mean "error"?

rhag posted:

I'm completely lost. So, you use maven, you put unit tests in test/java and maven won't run them? How is that possible?

Something got lost in translation and reading I'm sure, haha. No worries, I got it working :).

FAT32 SHAMER
Aug 16, 2012



I am supposed to edit class RefUnsortedList to include method endInsert.

Homework posted:

Design another public method to be added to the RefUnsortedList class, which inserts an
element at the end of the list.
 Do not add any instance variables to the existing class.
 The method signature is:
public void endInsert( Object element)
 Use your UseLists.java class that you created for the previous problem to test your method
 add method is demonstrated below so that it is clear the elements are added to the beginning of the list.

here is RefUnsortedList:
Java code:

package Homework;
//----------------------------------------------------------------------------
// RefUnsortedList.java          by Dale/Joyce/Weems                 Chapter 6
//
// Implements the UnsortedListInterface using references (a linked list).
//----------------------------------------------------------------------------



public class RefUnsortedList extends RefList implements UnsortedListInterface  
{

  public RefUnsortedList() 
  {
    super();
  }

  public void add(Object element)
  // Adds element to this list.
  {
    LLObjectNode newNode = new LLObjectNode(element);
    newNode.setLink(list);
    list = newNode;
    numElements++;
  }
  
  public int removeAll( Object element )
  {
      if (numElements == 0)
      {
          System.out.println ("Cannot delete from an empty list.");
      }
      
      else
      {
          while( numElements > 0  )
          {
              list = null;
              numElements--;
              reset();
          }
      }
      return numElements;
  }
  
  public void endInsert( Object element )
  {
      LLObjectNode newNode = new LLObjectNode( element );
  
      while( list != null)
      {
          list = previous;
      }
      newNode.setLink( list );
      list = newNode;
      numElements++;
  }
}

Here is the RefList that it extends:
Java code:


package Homework;

//-------------------------------------------------------------------------
// RefList.java            by Dale/Joyce/Weems                    Chapter 6
//
// Defines constructs for an unbounded reference-based list of objects that
// do not depend on whether the list is unsorted or sorted.
//
// Our intention is for this class to be extended by classes that furnish
// the remaining methods needed to support a list - for example, a method
// that allows objects to be added to the list.
//
// Null elements are not permitted on a list.
//
// One constructor is provided, one that creates an empty list.
//------------------------------------------------------------------------





public class RefList
{
  protected int numElements;          // number of elements in this list
  protected LLObjectNode currentPos;  // current position for iteration

  // set by find method
  protected boolean found;         // true if element found, else false
  protected LLObjectNode location; // node containing element, if found
  protected LLObjectNode previous; // node preceeding location

  protected LLObjectNode list;     // first node on the list

  public RefList()
  {
    numElements = 0;
    list = null;
    currentPos = null;
  }

  protected void find(Object target)
  // Searches list for an occurence of an element e such that
  // e.equals(target). If successful, sets instance variables
  // found to true, location to node containing e, and previous
  // to the node that links to location. If not successful, sets 
  // found to false.
  {
    boolean moreToSearch;
    location = list;
    found = false;

    moreToSearch = (location != null);
    while (moreToSearch && !found) 
    {
      if (location.getInfo().equals(target))  // if they match
       found = true;
      else
      {
        previous = location;
        location = location.getLink();
        moreToSearch = (location != null);
      }
    }
  }

  public int size()
  // Returns the number of elements on this list. 
  {
    return numElements;
  }

  public boolean contains (Object element)
  // Returns true if this list contains an element e such that 
  // e.equals(element); otherwise, returns false.
  {
    find(element);
    return found;
  }

  public boolean remove (Object element)
  // Removes an element e from this list such that e.equals(element)
  // and returns true; if no such element exists, returns false.
  {
    find(element);
    if (found)
    {
      if (list == location)     
        list = list.getLink();    // remove first node
      else
        previous.setLink(location.getLink());  // remove node at location

      numElements--;
    }
    return found;
  }

  public Object get(Object element)
  // Returns an element e from this list such that e.equals(element);
  // if no such element exists, returns null.
  {
    find(element);    
    if (found)
      return location.getInfo();
    else
      return null;
  }
  
  public String toString()
  // Returns a nicely formatted string that represents this list.
  {
    LLObjectNode currNode = list;
    String listString = "List:\n";
    while (currNode != null)
    {
      listString = listString + "  " + currNode.getInfo() + "\n";
      currNode = currNode.getLink();
    }
    return listString;
  }  

  public void reset()
  // Initializes current position for an iteration through this list,
  // to the first element on this list.
  {
    currentPos  = list;
  }

  public Object getNext()
  // Preconditions: The list is not empty
  //                The list has been reset
  //                The list has not been modified since most recent reset
  //
  // Returns the element at the current position on this list.
  // If the current position is the last element, then it advances the value 
  // of the current position to the first element; otherwise, it advances
  // the value of the current position to the next element.
  {
    Object next = currentPos.getInfo();
    if (currentPos.getLink() == null)
      currentPos = list;
    else
      currentPos = currentPos.getLink();
    return next;
  }
}
Here is the node class:
Java code:


package Homework;

//----------------------------------------------------------------------------
// LLObjectNode.java            by Dale/Joyce/Weems                  Chapter 3
//
// Implements Object nodes for a Linked List.
//----------------------------------------------------------------------------



public class LLObjectNode 
{
  private LLObjectNode link;
  private Object info;
  
  public LLObjectNode(Object info)
  {
    this.info = info;
    link = null;
  }
 
  public void setInfo(Object info)
  // Sets info Object of this LLObjectNode.
  {
    this.info = info;
  }

  public Object getInfo()
  // Returns info Object of this LLObjectNode.
  {
    return info;
  }
 
  public void setLink(LLObjectNode link)
  // Sets link of this LLObjectNode.
  {
    this.link = link;
  }

  public LLObjectNode getLink()
  // Returns link of this LLObjectNode.
  {
    return link;
  }
}
 
 

And here is the client code:
Java code:


package Homework;


public class UseLists
{
    public static void main(String[] args)
    {
        RefUnsortedList list = new RefUnsortedList();
        
        list.add( "Mark" );
        list.add( "Joel" );
        list.add( "Kathy" );
        list.add( "Susan" );
        list.add( "Jim" );
        
        System.out.print( list );
        
        list.endInsert( "Julien");
        System.out.print( list );
        
        list.endInsert( "David" );
        System.out.print( list );
        
        list.endInsert( "Tim" );
        System.out.print( list );
        
        
        System.out.println( "\nTesting ReferenceUnsortedList removeAll method : ");
        
        list.removeAll( list );
        System.out.printf( "List contains: %d elements\n", list.numElements );
        
    }
}

I think I'm on the right track with method endInsert, however since I don't have next and previous, I'm not sure how I'm supposed to cycle through the list until I reach a node that is of value null (or however I should find the end of the list) to then insert the newNode into. Can someone please smack me upside the head, call me an idiot, and suggest how I may do this without adding any instance variables to the existing class? I've been looking through all of the methods of RefList and while getNext() seems like it would work, it isn't of type LLObjectNode and obviously casting it as type [fixed]Object/fixed] won't solve anything.

FAT32 SHAMER fucked around with this message at 05:41 on Apr 4, 2014

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!
It looks to me like the 'find' method does something pretty similar to what you're looking for (except that it doesn't insert, obviously).

FAT32 SHAMER
Aug 16, 2012



Kilson posted:

It looks to me like the 'find' method does something pretty similar to what you're looking for (except that it doesn't insert, obviously).

I did try that, however how would I use it? I tried while( list != null ) { find( null); } and the programme compiles, but it sits there and doesn't finish like it's looping to infinity.

would it be

Java code:
while( list != null )
{
	if( find( null ) == null
	{
		list = find( null );
	}
	else
		return;

oh but find is of type Object void so :downsgun:

FAT32 SHAMER fucked around with this message at 07:43 on Apr 4, 2014

FAT32 SHAMER
Aug 16, 2012



I figured it out, though I'm positive there's a better way to do it than using an if/else statement to do the same thing either way.

Java code:
  public void endInsert( Object element )
  {
      LLObjectNode newNode = new LLObjectNode( element );
      boolean moreToSearch;
      Comparable listElement;
      LLObjectNode prevLoc;     // trailing referenc
      
      location = list;
      moreToSearch = ( location != null );
      prevLoc = null;

      while( moreToSearch )
      {
          listElement = ( Comparable )location.getInfo();
          
          if ( listElement.compareTo( element ) < 0 )  // list element < add element
          {
              prevLoc = location;
              location = location.getLink();
              moreToSearch = ( location != null );   // stop looking at end of list
          }
          
          else
          {
              prevLoc = location;
              location = location.getLink();
              moreToSearch = ( location != null );   // stop looking at end of list
          }   
     }

      
      if ( prevLoc == null )   
      {
          newNode.setLink(list);
          list = newNode;          
      }
      else
      {
          newNode.setLink( location );
          prevLoc.setLink(newNode);
      }
      numElements++;

  }
}

pigdog
Apr 23, 2004

by Smythe

Tusen Takk posted:

I figured it out, though I'm positive there's a better way to do it than using an if/else statement to do the same thing either way.
Uh, yes? Such if statement is completely useless; remove it and leave the contents of one of the options.

FAT32 SHAMER
Aug 16, 2012



pigdog posted:

Uh, yes? Such if statement is completely useless; remove it and leave the contents of one of the options.

This is why you shouldn't stay up until 6a doing homework :morning:

pigdog
Apr 23, 2004

by Smythe

Tusen Takk posted:

This is why you shouldn't stay up until 6a doing homework :morning:

So that's why you're writing a public void endInsert( Object element ) method while your RefUnsortedList class already has one?

FAT32 SHAMER
Aug 16, 2012



pigdog posted:

So that's why you're writing a public void endInsert( Object element ) method while your RefUnsortedList class already has one?

No, I had to add that method to RefUnsortedList and that's where that snippet is from.

Smarmy Coworker
May 10, 2008

by XyloJW
Java code:
		File a = new File(args[2]);
		if (!a.exists()) {
			try {
				a.mkdirs();
				a.createNewFile();
			} catch (Exception e) {
				System.err.print("Could not create a");
				System.exit(1);
			}
		}
		if (!a.canWrite()) {
			System.err.print("Cannot write to a");
			System.exit(1);
		}
do I care about the exception type here or what? I don't think I do since I'm just going to ERR and exit but I want to be sure :)

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

Smarmy Coworker posted:

Java code:
		File a = new File(args[2]);
		if (!a.exists()) {
			try {
				a.mkdirs();
				a.createNewFile();
			} catch (Exception e) {
				System.err.print("Could not create a");
				System.exit(1);
			}
		}
		if (!a.canWrite()) {
			System.err.print("Cannot write to a");
			System.exit(1);
		}
do I care about the exception type here or what? I don't think I do since I'm just going to ERR and exit but I want to be sure :)

Yes.

1: always catch the most specific exception visible. This way, unchecked exceptions can properly propagate instead of getting squelched, among other reasons.

2: Don't use system.exit. either rethrow the exception or don't catch it in the first place, if this is something that should end your flow.

Smarmy Coworker
May 10, 2008

by XyloJW
project guidelines tells me "print one line to system.err and exit. and only do this."
but good point on #1, I've cleaned up to catch IOExceptions and some others

Smarmy Coworker fucked around with this message at 18:50 on Apr 4, 2014

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

Smarmy Coworker posted:

project guidelines tells me "print one line to system.err and exit. and only do this."
but good point on #1, I've cleaned up to catch IOExceptions and some others

Then do it for this class only, and note that you'd be crucified if you did that in a real job as "the rear end in a top hat that made the server mysteriously crash".

loving Academia :sigh:

Volguus
Mar 3, 2009

Smarmy Coworker posted:

project guidelines tells me "print one line to system.err and exit. and only do this."
but good point on #1, I've cleaned up to catch IOExceptions and some others

You can return from main and the program will exit. You don't have to specifically call System.exit to do this.

Smarmy Coworker
May 10, 2008

by XyloJW

rhag posted:

You can return from main and the program will exit. You don't have to specifically call System.exit to do this.

:eyepop:

BirdOfPlay
Feb 19, 2012

THUNDERDOME LOSER

rhag posted:

You can return from main and the program will exit. You don't have to specifically call System.exit to do this.

It's been ages since I did Java, but does System.exit put up any happy messages or is it the same program end as return?

For my question, I'm getting started with Java to knock some rust off and gain some experience with a larger sized project in the language. I made a little maze game in C++ and want to build a mapmaker for the game in Java.

I know the OP recommends Eclipse (with or without Netbeans, I assume), but does anyone use DrJava as an IDE? It's a light-weight .jar and supports some simple project management. I could use Eclipse, but my understanding is it takes some TLC to get setup.

As far as the mapmaker is concerned relearning the primitives and how they interact has lead me to be greatly concerned. The map files are not human readable and just a simple collection of bytes: the first two define the size of the map and each subsequent byte defines a single space on a grid. I did this to avoid having to create an overly complicated parser and to keep things simple (the largest map allowed is 16x16). For what it's worth I used bit masking to encode the information for each space.

First and foremost, is there a reason that I shouldn't be using Java if I working at this low of a level? When prepping for this project I cam across this Oracle tutorial advising against using the byte streams. Granted that could just be in the context of reading characters, since Java is UTF-16 from the bottom up.

Also, when using the byte streams should I expect strange behavior when reading a single byte at a time? For example 0x80 is a byte that occurs in all maps (defines the border), will they read and write that correctly? I ask, because Java pitched a fit at byte = 0x80 but was fine with byte = -127.

ulmont
Sep 15, 2010

IF I EVER MISS VOTING IN AN ELECTION (EVEN AMERICAN IDOL) ,OR HAVE UNPAID PARKING TICKETS, PLEASE TAKE AWAY MY FRANCHISE

BirdOfPlay posted:

It's been ages since I did Java, but does System.exit put up any happy messages or is it the same program end as return?

Returning out of main will give you exit code 0. System.exit(n) can give you another code (traditionally anything nonzero is an error).

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
If you're reading a file containing binary data, you'll want to get in the habit of using a DataInputStream rather than operating on a raw InputStream.

0x80 is not a byte literal, it's an int literal- one that would not fit in a signed byte. That Java does not have a proper byte literal is a bit of an annoyance, but you can do what you want by using "((byte)0x80)".

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.
Bytes in java are going to be confusing if you come from a C background, because they are values within the range of -128 to 127. Java wants to interpret 0x80 as an int - it's value is 0x00000080, or 128, which is too big to fit in a byte (in the same way that 1024 is too big to fit in a byte.)

Beyond how you represent literals in your source code, java is fine for manipulating bytes. It's not like the language gets confused by certain patterns of bits. The tutorial is warning you that a byte stream is the wrong tool for the job of manipulating text - you should you a character stream.

As far as I'm aware, IntelliJ and Eclipse are the standard IDEs. When I install a fresh Eclipse, I immediately download the m2e Maven plugin and a git plugin, and then set tabs to be replaced by 4 spaces. I consider it "usable" at this point, and feel the rest out as I go.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
It's me, I'm the last hold out using NetBeans :(

I just like the UI better than eclipse and it's builtin maven support seals the deal. I use eclipse for Android stuff, but I can't get over the UI.

Brain Candy
May 18, 2006

Janitor Prime posted:

It's me, I'm the last hold out using NetBeans :(

I just like the UI better than eclipse and it's builtin maven support seals the deal. I use eclipse for Android stuff, but I can't get over the UI.

Look man, get your eyes checked. I tried to use NetBeans for a thing, hoping it had gotten better, but I actually laughed out loud at the terrible lowest bidder swing API in 2014.

Eclipse has maven support in 30 seconds. Please stop defending your abuser, it's not cool. :smith:

baquerd
Jul 2, 2007

by FactsAreUseless
For people who are professional Java developers, IntelliJ basically takes Eclipse and makes it look like a little crying child that you need to take care of all the time.

OK, it's not that bad and IDE wars are holy wars, etc.

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

baquerd posted:

For people who are professional Java developers, IntelliJ basically takes Eclipse and makes it look like a little crying child that you need to take care of all the time.

OK, it's not that bad and IDE wars are holy wars, etc.

I never figured out how to get per-thread stepping to work with Android Studio (read: IntelliJ). Which is kind of a big deal when doing multithreaded debugging.

Doctor w-rw-rw-
Jun 24, 2008

Gravity Pike posted:

As far as I'm aware, IntelliJ and Eclipse are the standard IDEs. When I install a fresh Eclipse, I immediately download the m2e Maven plugin and a git plugin, and then set tabs to be replaced by 4 spaces. I consider it "usable" at this point, and feel the rest out as I go.
m2e is included in Eclipse as of a couple of years ago.

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.
I literally installed Kepler today, and it didn't come with. :confused:

BirdOfPlay
Feb 19, 2012

THUNDERDOME LOSER
Thanks for the help, but what the gently caress is this ("Modified UTF-8") poo poo?

So, when it reads "0x80," it returns "0xC480." Or for "0xF8" you get "0xC7B8." :what:

EDIT: Whoops, readByte() works as expected, I thought there was just the read() method. But still, why would that be the simple solution to anything? How many ops does it take to recajigger the original byte?

BirdOfPlay fucked around with this message at 09:56 on Apr 6, 2014

Gul Banana
Nov 28, 2003

java is targeted at applications where reading characters and human-scale numbers and Web Things are more common than dealing with raw byte layouts. that doesn't mean you can't use it for the latter but the 'easy path' through many apis is going to be higher level than you'd prefer.

an alternative would be to make dealing with packed bits easy and xml-rpc hard, but there are already languages for that.

Brain Candy
May 18, 2006

BirdOfPlay posted:

Thanks for the help, but what the gently caress is this ("Modified UTF-8") poo poo?

So, when it reads "0x80," it returns "0xC480." Or for "0xF8" you get "0xC7B8." :what:

EDIT: Whoops, readByte() works as expected, I thought there was just the read() method. But still, why would that be the simple solution to anything? How many ops does it take to recajigger the original byte?

Okay, you don't want streams. If you are cool with buffers and understand that you need to check return values, use FileChannel. It is the thinnest of thin wrappers. You read bytes into a buffer of bytes called a... ByteBuffer. :v:

Brain Candy fucked around with this message at 12:50 on Apr 6, 2014

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Gravity Pike posted:

I literally installed Kepler today, and it didn't come with. :confused:

It's in the Java EE version.

After being forced to use IntelliJ for like 8 months I still prefer Eclipse.

Smarmy Coworker
May 10, 2008

by XyloJW
yesterday i discovered java.nio.file.Files and it's pretty cool. I enjoy the ability to blast a Files.readAllBytes without having to instantiate an input stream
what OTher c00l stuff is there??

Jose Cuervo
Aug 25, 2004
I have been programming in Python but now am trying to modify someones code that is in Java. Is there a reason that this person would not have used this.ID instead of ID in the getID() function? I cannot tell why they would have omitted it.

Java code:
/*
Copyright 2008 Nick Malleson

This file is part of RepastCity.

RepastCity is free software: you can redistribute it and/or modify it under the terms of 
the GNU General Public License as published by the Free Software Foundation, either version 
3 of the License, or (at your option) any later version.

RepastCity is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with RepastCity.  
If not, see <[url]http://www.gnu.org/licenses/[/url]>.
 */
package repastcity.citycontext;

import java.util.ArrayList;
import repastcity.ContextCreator;
import com.vividsolutions.jts.geom.Coordinate;

public class Junction {

	private int ID;  //	private String identifier;
	private Coordinate coord;
	
	private ArrayList<Road> roads = new ArrayList<Road>();	// The Roads connected to this Junction

	public Junction() {
		this.ID = ContextCreator.generateAgentID();
	}
	
	public Junction(Coordinate coord, Road road) {
		this.ID = ContextCreator.generateAgentID();
		this.coord = coord;
	}
	
	public String toString() {
		return "Junction "+this.ID+" at: "+this.coord.toString(); 
	}

	public int getID() {
		return ID;
	}
	public void setID(int id) {
		this.ID = id;
	}
	
	public Coordinate getCoordinate() {
		return this.coord;
	}
	
	public boolean equals(Junction j) {
		if (this.coord.equals(j.getCoordinate()))
			return true;
		else
			return false;
	}

	public ArrayList<Road> getRoads() {
		return this.roads;
	}
	public void addRoad(Road road) {
		this.roads.add(road);
	}
}

JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

Jose Cuervo posted:

I have been programming in Python but now am trying to modify someones code that is in Java. Is there a reason that this person would not have used this.ID instead of ID in the getID() function? I cannot tell why they would have omitted it.

Pure code style - there's no difference as there's no other ID variable in scope.

If you have a class like this:

Java code:
public class Junction {

	private int ID;	

	public int getID() {
		return ID;
	}
	public void setID(int ID) {
		this.ID = ID;
	}
}
Then in the setID method ID refers to the parameter being passed in while this.ID refers to the private variable which the Junction object owns. Note I've had to change the case of the setID parameter as variable are case sensitive - in your example setID could easily just be:

Java code:
public void setID(int id) {
    ID = id;
}

Smarmy Coworker
May 10, 2008

by XyloJW

Jose Cuervo posted:

I have been programming in Python but now am trying to modify someones code that is in Java. Is there a reason that this person would not have used this.ID instead of ID in the getID() function? I cannot tell why they would have omitted it.

unlike python classes any defined parameters are always in scope
if this person had instantiated another ID variable in a method (not getID because that would be dumb, but i guess this would ALSO be dumb anywhere else, except for javadocs looking nice, but i think capitalization matters so they should just use 'id' for that?) they would then have to use this.ID to access the parameter. So basically,

JingleBells posted:

Pure code style - there's no difference as there's no other ID variable in scope.

Smarmy Coworker fucked around with this message at 23:44 on Apr 7, 2014

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Yeah, in Java this. is only needed if there is another variable of the same name in local scope and you're accessing the instance variable. Notably, calling this.ID does not invoke a getter method like it does in some languages.

Adbot
ADBOT LOVES YOU

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...
That said, getters and setters can be important. If you have a subclass that has slightly different behavior in the getter/setter, you're potentially causing subtle bugs by referencing the variable directly.

:backtowork:

Mainly, it depends on code style, both of the developer and of the codebase. If you're working on Enterprise Software, you should probably always use getters/setters.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply