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
SmellsLikeToast
Dec 30, 2005

A GREATER MAN THAN I DESERVE

carry on then posted:

You're closer than you think. If you want selection to be set only when the currentitem matches the one you're looking for, when should you be setting selection?

Inside the if, I would think.

Adbot
ADBOT LOVES YOU

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

SmellsLikeToast posted:

Inside the if, I would think.

Give it a shot.

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

If you don't see what carry on then is getting at, look at what each line of your loop is doing - write it out in plain English, so you've got a sequence of steps. Imagine you have 3 items in your list, and the 2nd one is the item that matches. Walk through your loop step by step as you iterate over the whole list, and look at exactly what's happening at each step.

You can watch this by stepping through with a debugger too, but puzzling out the logic is a big part of learning to 'think right'

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Yeah, let me know if I'm being too vague. When it's assignment help I like to walk people through the thought process to see where the misunderstanding is, but if I'm off base it'll seem like I'm just oblivious :v:

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Yeah I think you two were talking past each other a minute ago :v:

SmellsLikeToast
Dec 30, 2005

A GREATER MAN THAN I DESERVE

carry on then posted:

Yeah, let me know if I'm being too vague. When it's assignment help I like to walk people through the thought process to see where the misunderstanding is, but if I'm off base it'll seem like I'm just oblivious :v:

Oh, no, you're fine. I'd much rather be pointed in the right direction than spoon fed. I changed the code so that it's changed in the if, but it's still not working. Guess I must have a logic error in getSelection I need to search for now. Either that or I missed something in the constructor and/or instantiated the Item object incorrectly. Gonna check all that.

SmellsLikeToast fucked around with this message at 21:49 on Feb 15, 2016

Edgar Allan Pwned
Apr 4, 2011

Quoth the Raven "I love the power glove. It's so bad..."
Hey, I'm looking for some good sites or books that have practices for Java and learning Object Oriented Programming. Does anyone have any favorites?

Fergus Mac Roich
Nov 5, 2008

Soiled Meat

Edgar Allan Pwned posted:

Hey, I'm looking for some good sites or books that have practices for Java and learning Object Oriented Programming. Does anyone have any favorites?

Oracle's Java tutorials are an excellent place to start.

Edward_Tohr
Aug 11, 2012

In lieu of meaningful text, I'm just going to mention I've been exploding all day and now it hurts to breathe, so I'm sure you all understand.
I'm working on a simple Enigma emulator at the moment. I have five instances a Rotor class that holds the cypher key and rollover point for each of the five main rotors. The problem is, I'm not sure how to allow the user to change the rotors at runtime instead of compile-time. Would moving the code that sets up the cypher key and rollover position to the options menu be a workable solution, or is there a more elegant way that I'm just too dumb to see?

Admittedly, I am pretty dumb when it comes to programming stuff more complicated than Hello World.

EDIT: the code's available at https://github.com/edward-tohr/enigma-java if anyone wants to take a look.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

Edward_Tohr posted:

I'm working on a simple Enigma emulator at the moment. I have five instances a Rotor class that holds the cypher key and rollover point for each of the five main rotors. The problem is, I'm not sure how to allow the user to change the rotors at runtime instead of compile-time. Would moving the code that sets up the cypher key and rollover position to the options menu be a workable solution, or is there a more elegant way that I'm just too dumb to see?

Admittedly, I am pretty dumb when it comes to programming stuff more complicated than Hello World.

EDIT: the code's available at https://github.com/edward-tohr/enigma-java if anyone wants to take a look.

Just moving the code itself isn't enough, you'd have to kinda change it. May as well leave that code as-is as like, default values, and then let the user overwrite those. Or does that not make sense?

You just need to add an option that lets the user pick one of the rotors, choose if they want to set the cypher or rollover, and then let them enter in a string. Then you parse the rotor they picked as an integer, and set that rotor's cypher to the string they entered, or parse the string and set the rotor's rollover to that integer if they chose rollover.

Does that make sense?

Another way to change the values at run-time instead of compiling it again would be to read the values in from a text file. Then the user can modify the text file easily.

TheresaJayne
Jul 1, 2011

Edward_Tohr posted:

I'm working on a simple Enigma emulator at the moment. I have five instances a Rotor class that holds the cypher key and rollover point for each of the five main rotors. The problem is, I'm not sure how to allow the user to change the rotors at runtime instead of compile-time. Would moving the code that sets up the cypher key and rollover position to the options menu be a workable solution, or is there a more elegant way that I'm just too dumb to see?

Admittedly, I am pretty dumb when it comes to programming stuff more complicated than Hello World.

EDIT: the code's available at https://github.com/edward-tohr/enigma-java if anyone wants to take a look.

not looked at the code yet but I would have the rotor class and define x rotors, then have the machine itself and each slot in the machine can have a rotor added in (as in the real machine)
and then the slots hold the details of which rotor and which mode it is in.

The rotor doesnt know what the setting on the machine is. remember the OO setup
you put the rotor in slot 1,
you then tell the slot to rotate to position x (so set the rotor to the specific start position)
then you start encoding -

I do not know the full setup of how it works but if you define some hard coded rotors that you place in the machine at each slot position, then you rotate them to the setting position, then you use them - each rotor then knows the symbols on the rotor and how many (for the rollover i guess)

Edward_Tohr
Aug 11, 2012

In lieu of meaningful text, I'm just going to mention I've been exploding all day and now it hurts to breathe, so I'm sure you all understand.
Turns out I'm a loving moron, and my question would have been better phrased as "how do I instantiate a member of a class such that methods outside of main() can access it?"

On the bright side, I finally understand what "static" actually means. :v:

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

Edward_Tohr posted:

Turns out I'm a loving moron, and my question would have been better phrased as "how do I instantiate a member of a class such that methods outside of main() can access it?"

On the bright side, I finally understand what "static" actually means. :v:

The difference between static and dynamic really confuses a lot of new programmers, and understandably. Its OOP and if you're just learning programming then OOP is overwhelming, you're busy doing simple programming and now you've gotta worry about static classes versus invoking objects, and then you have situations that are really confusing like, you have a non-static Main class, and the Main class has a static main() method, and the static main() method creates an instanced object of type Main and then calls Main.someMethod(); in order to pass over to a dynamic version of the same class from the static context... can be confusing!

I remember in high school I had this game library I'd downloaded, and I was trying to write some change into this game I was doing, and I tried calling a method and it gave me the static / dynamic context issue in the compiler, so what I did was I went and changed the method to static. But then there was some method calling that method, so I made those static too, on and on and on down the tree of method calls. It was horrible.

I'm not sure what you did to solve it though. What you can do is make whatever static, and then anything can get a static reference to that object easily and call those static methods. But that's approaching what we call a "Singleton" design pattern, which is usually not actually what you want and can be a bad thing.

Instead, if you need to create a dynamic object and then allow other methods outside of main() to access it, what you have to do is pass a reference to that object as a parameter. That or you can have a public method to retrieve the object, and then have the method outside of main() call that method which returns a reference to the object.

Edward_Tohr
Aug 11, 2012

In lieu of meaningful text, I'm just going to mention I've been exploding all day and now it hurts to breathe, so I'm sure you all understand.

Zaphod42 posted:

What you can do is make whatever static, and then anything can get a static reference to that object easily and call those static methods. But that's approaching what we call a "Singleton" design pattern, which is usually not actually what you want and can be a bad thing.

Instead, if you need to create a dynamic object and then allow other methods outside of main() to access it, what you have to do is pass a reference to that object as a parameter. That or you can have a public method to retrieve the object, and then have the method outside of main() call that method which returns a reference to the object.

My thought process here is that since each rotor object represents a single, specific physical object, it makes sense for them to be static in this specific instance. Does that sound right?

Fergus Mac Roich
Nov 5, 2008

Soiled Meat

Edward_Tohr posted:

My thought process here is that since each rotor object represents a single, specific physical object, it makes sense for them to be static in this specific instance. Does that sound right?

No, that doesn't indicate a reason to make them static. Static means that the variable(or nested class, or a method, or what have you) isn't associated with a particular object of the enclosing class. Make sense? So making them static means that different instances of Enigma don't have their own Rotor objects, there are just Rotor variables in the Enigma "namespace". In this case you might be able to get your code working with static variables, but I recommend you remove static from all of those for now. There is a more correct way to go about what you're doing.

This is a little confusing because main() is itself a static method. So at the beginning of code execution, there is no Enigma object, and thus, without any static variables, no Rotor objects. Do you think you can figure it out from there?

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Edward_Tohr posted:

My thought process here is that since each rotor object represents a single, specific physical object, it makes sense for them to be static in this specific instance. Does that sound right?

When you have static members of a class (like all your static rotor variables) that means that every instance of that class shares them. So if you have two instances of Enigma, and you set new Rotors on one of them, both Enigmas will see the same Rotors. (Marking them static means they actually become part of the Enigma class itself, not any instance of it)

That might be what you want, but I'm guessing probably you don't. If you make two Enigma objects for whatever reason, would you expect them to be completely independent? If you set the Rotors on one, you wouldn't want that to affect the configuration of the other, right?

On the other hand, your Rotor definitions (r1 etc.) probably make sense to be static. They're like six fixed configurations that can be used, it's a set that's common to all Enigmas, so that's a good thing to share between them all. Since they don't change, it's a good idea to make them static final, partly so you don't actually change them but also so it's clear that they're fixed, constant elements. If you are planning to let the user change them, this obviously doesn't apply!

Edward_Tohr
Aug 11, 2012

In lieu of meaningful text, I'm just going to mention I've been exploding all day and now it hurts to breathe, so I'm sure you all understand.
Okay, this is starting to make more sense to me now, I think. :v:

Thanks for the help, everyone.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.
Basically, your main() is static. From there, main() should create an Enigma object instance (instance meaning dynamic, not static), and then either Enigma builds Rotor instances or main() builds rotor instances and then passes them to Enigma, and Enigma holds a reference to them.

It may actually help clarify things to break main() out into a separate class.

code:
class Main
{
    //Static method
    public static void main(String[] args){
        //creating new instance of Enigma class
        Enigma mainEnigma = new Enigma();

        Rotor rot1 = new Rotor("EKMFLGDQVZNTOWYHXUSPAIBRCJ",0,17);

        //repeat this for each rotor

        //calling method on instance
        mainEnigma.enigma(rot1, rot2, rot3, reflector, plugboard);
    }

}
That's about how it should look. Then the mainEnigma instance of Enigma would handle processing from there.

That should help clarify things. There's nothing to the Main class other than the main() method, so you don't have to think about if Main is static or dynamic, it'll always just be a static main() method to get things started, but then from there you roll up instances and invoke methods on them. Most things other than main() should not be static unless you have a reason for doing so.

Zaphod42 fucked around with this message at 22:17 on Feb 18, 2016

HFX
Nov 29, 2004
Are their any issues with threading with the following code? Specifically, I am wondering if I can trust the HttpServletRequest to be injected by the same request as ContainerReqeust. Also, can I be guaranteed, the HttpServletRequest will not gain a new context in the middle of executing the filter method?

code:
public class TestFilter implements ContainerRequestFilter {
  @Context
  private HttpServletRequest httpServletRequest;
  
  @Override
  public ContainerRequest filter(ContainerRequest request) {
    return null;
  }
}
I am digging into a front end production issue and it has been a while since I reviewed thread safety and using Jersey restful webservices.

Edit: Nevermind, I found the answer. For everyones reference, it should be safe.

HFX fucked around with this message at 00:36 on Feb 19, 2016

gurney
Feb 17, 2016

by Shine
Why is this not working?

Not sure what the etiquette is on asking for help in this thread on school work but any tips/hints would be appreciated!

Background: One class in python. Currently in a course on Java. Assignment has various parts, but I'm stuck on one part.

I must use two classes, one called Roster and the other called Student. I'm required to create Students objects from a string array, and then read these objects to the console.

code:

public class Student {
	
	private String student_ID;
	private String first_name;
	private String last_name;
	private String email;
	private int age;
	private int grade1;
	private int grade2;
	private int grade3;
	
	
	
	
	
	
	public Student(String student_ID, String first_name, String last_name, String email, int age, int grade1,
			int grade2, int grade3) {
		
		this.student_ID = student_ID;
		this.first_name = first_name;
		this.last_name = last_name;
		this.email = email;
		this.age = age;
		this.grade1 = grade1;
		this.grade2 = grade2;
		this.grade3 = grade3;
		
	}
		
		
	
	
		
		
		
		
		
		
	



	public String getstudent_ID() {
		return student_ID;
	}
	public String getfirst_name() {
		return first_name;
	}
	public String getlast_name() {
		return last_name;
	}
	public String getEmail() {
		return email;
	}
	public int getage() {
		return age;
	}
	public int getgrade1() {
		
		return grade1;
		
		}
	
	public int getgrade2() {
		return grade2;
	}
	
	public int getgrade3() {
		return grade3;
	}
	
	public void setstudent_ID(String student_ID) {this.student_ID = student_ID;}
	public void setfirst_name(String first_name){this.first_name = first_name;}
	public void setlast_name(String last_name){this.last_name = last_name;}
	public void setemail(String email){this.email = email;}
	public void setage(int age){this.age = age;}
	public void setgrade1(int grade1){this.grade1 = grade1;}
	public void setgrade2(int grade2){this.grade1 = grade2;}
	public void setgrade3(int grade3){this.grade1 = grade3;}
	

	
}
	

	
code:
import java.util.ArrayList;


public class Roster { 
	
	

	static String [] students = {"1, John, Smith, [email]John1989@gmail.com[/email],20,88,79,59",
			   "2, Suzan, Erickson, [email]Erickson_1990@gmail.com[/email],19,91,72,85",
			   "3, Jack, Napoli, The_lawyer99yahoo.com,19,85,84,87",
			   "4, Erin, Black, [email]Erin.Black@comcast.net[/email],22,91,98,82""};
	
	
		
	
	
	static ArrayList<Student> mystudents = new ArrayList<Student>();
		
	
	public static void add(String student_ID, String first_name, String last_name, String email, int age, int grade1,
			int grade2, int grade3){
		
		Student k = new Student(student_ID, first_name, last_name, email, age, grade1, grade2, grade3);
		mystudents.add(k);
	}
		



	public static void main(String[] args) {
		for (int i = 0; i < students.length; i++){
		String q = students[i];
		String[] hold = q.split(",");
		String student_ID = hold[0];
		String first_name = hold[1];
		String last_name = hold[2];
		String email = hold[3];
		int age = Integer.parseInt(hold[4]);
		int grade1 = Integer.parseInt(hold[5]);
		int grade2 = Integer.parseInt(hold[6]);
		int grade3 = Integer.parseInt(hold[7]);
		
		Student h = new Student(student_ID, first_name, last_name, email, age, grade1, grade2, grade3);
		mystudents.add(h);
		}
		
	   for(Student j: mystudents) {
		   System.out.print(j);
	   }
	
	
	
	
	}
	
	
}

The output I get when I execute the above is:

quote:

Student@2a139a55Student@15db9742Student@6d06d69cStudent@7852e922Student@4e25154f

When I should be getting something like this:

quote:

"1, John, Smith, John1989@gmail.com,20,88,79,59",
"2, Suzan, Erickson,Erickson_1990@gmail.com,19,91,72,85",
"3, Jack, Napoli, The_lawyer99yahoo.com,19,85,84,87",
"4, Erin, Black,Erin.Black@comcast.net,22,91,98,82""};

gurney fucked around with this message at 06:47 on Feb 19, 2016

HFX
Nov 29, 2004

gurney posted:

Why is this not working?

Not sure what the etiquette is on asking for help in this thread on school work but any tips/hints would be appreciated!

Background: One class in python. Currently in a course on Java. Assignment has various parts, but I'm stuck on one part.

I must use two classes, one called Roster and the other called Student. I'm required to create Students objects from a string array, and then read these objects to the console.

code:

public class Student {
	
	private String student_ID;
	private String first_name;
	private String last_name;
	private String email;
	private int age;
	private int grade1;
	private int grade2;
	private int grade3;
	
	
	
	
	
	
	public Student(String student_ID, String first_name, String last_name, String email, int age, int grade1,
			int grade2, int grade3) {
		
		this.student_ID = student_ID;
		this.first_name = first_name;
		this.last_name = last_name;
		this.email = email;
		this.age = age;
		this.grade1 = grade1;
		this.grade2 = grade2;
		this.grade3 = grade3;
		
	}
		
		
	
	
		
		
		
		
		
		
	



	public String getstudent_ID() {
		return student_ID;
	}
	public String getfirst_name() {
		return first_name;
	}
	public String getlast_name() {
		return last_name;
	}
	public String getEmail() {
		return email;
	}
	public int getage() {
		return age;
	}
	public int getgrade1() {
		
		return grade1;
		
		}
	
	public int getgrade2() {
		return grade2;
	}
	
	public int getgrade3() {
		return grade3;
	}
	
	public void setstudent_ID(String student_ID) {this.student_ID = student_ID;}
	public void setfirst_name(String first_name){this.first_name = first_name;}
	public void setlast_name(String last_name){this.last_name = last_name;}
	public void setemail(String email){this.email = email;}
	public void setage(int age){this.age = age;}
	public void setgrade1(int grade1){this.grade1 = grade1;}
	public void setgrade2(int grade2){this.grade1 = grade2;}
	public void setgrade3(int grade3){this.grade1 = grade3;}
	

	
}
	

	
code:
import java.util.ArrayList;


public class Roster { 
	
	

	static String [] students = {"1, John, Smith, [email]John1989@gmail.com[/email],20,88,79,59",
			   "2, Suzan, Erickson, [email]Erickson_1990@gmail.com[/email],19,91,72,85",
			   "3, Jack, Napoli, The_lawyer99yahoo.com,19,85,84,87",
			   "4, Erin, Black, [email]Erin.Black@comcast.net[/email],22,91,98,82""};
	
	
		
	
	
	static ArrayList<Student> mystudents = new ArrayList<Student>();
		
	
	public static void add(String student_ID, String first_name, String last_name, String email, int age, int grade1,
			int grade2, int grade3){
		
		Student k = new Student(student_ID, first_name, last_name, email, age, grade1, grade2, grade3);
		mystudents.add(k);
	}
		



	public static void main(String[] args) {
		for (int i = 0; i < students.length; i++){
		String q = students[i];
		String[] hold = q.split(",");
		String student_ID = hold[0];
		String first_name = hold[1];
		String last_name = hold[2];
		String email = hold[3];
		int age = Integer.parseInt(hold[4]);
		int grade1 = Integer.parseInt(hold[5]);
		int grade2 = Integer.parseInt(hold[6]);
		int grade3 = Integer.parseInt(hold[7]);
		
		Student h = new Student(student_ID, first_name, last_name, email, age, grade1, grade2, grade3);
		mystudents.add(h);
		}
		
	   for(Student j: mystudents) {
		   System.out.print(j);
	   }
	
	
	
	
	}
	
	
}

The output I get when I execute the above is:


When I should be getting something like this:

You need to override the toString() method. In general, any object that has stateful fields should override the toString(), hashCode(), and equals() methods. Fortunately, your IDE can do this for you. Unfortunately, you have to remember to redo it after adding / removing a member.

HFX fucked around with this message at 08:22 on Feb 19, 2016

gurney
Feb 17, 2016

by Shine
Thanks!

casual poster
Jun 29, 2009

So casual.

Edgar Allan Pwned posted:

Hey, I'm looking for some good sites or books that have practices for Java and learning Object Oriented Programming. Does anyone have any favorites?

First start on codeacademy. It'll take a few hours and it's informative. After that move onto mooc.fi, which is a Finnish university that has a few english courses here: http://mooc.fi/english.html You have to submit exercises and once you pass 85% of a week's exercises it lets you download the next weeks exercises. There's no time limit btw, they just section it off into weeks.
It's a 16 week program and it's free. You'll need to install Netbeans but it walks you through that pretty easily. Have fun!

Once you get into learning about creating objects (maybe week 2 or 3) get a book called Head First Java, it's very informative and uses illustrations to explain OOP concepts (it's good to get a second look at something) and has been a life saver for me (be sure to use the index in the back to find certain things!). Also brush up on your google and find some cool websites to hang out on. dreamincode is good, and so is java code geeks. When you get further in and have questions, start googling for the answer and remember to use the Oracle documents (they weren't very novice-friendly at the start, but you'll slowly begin to understand what they're talking about).
But first, get started on the mooc site. It will give you a solid understanding.

casual poster fucked around with this message at 11:40 on Feb 20, 2016

smackfu
Jun 7, 2004

Any opinions of Project Lombok? It uses annotations to replace boilerplate like getters and setters and isequals. Which is neat, but you end up with Java code that doesn't compile unless you are using a supported tool chain and IDE which weirds me out.

https://projectlombok.org/features/

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

smackfu posted:

Any opinions of Project Lombok? It uses annotations to replace boilerplate like getters and setters and isequals. Which is neat, but you end up with not-Java code

FTFY

My general opinion is that it doesn't give you any "big gains" that justify messing up your tooling and tying yourself to whatever they choose to support. Stuff like try-with-resources and AutoValue gives you the big wins, and the remaining stuff is pretty minor.

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.
We're using it mostly for the @Data, @Value (+ @Wither) and @Builder annotations. It works okay, but it has some awkward limitations like @Builder not supporting inheritance. It did reduce a lot of boilerplate though and so far tooling has been mostly fine with it. The main tooling annoyance I have is that it doesn't work with JavaDoc without a Delombok step.

Volguus
Mar 3, 2009

smackfu posted:

Any opinions of Project Lombok? It uses annotations to replace boilerplate like getters and setters and isequals. Which is neat, but you end up with Java code that doesn't compile unless you are using a supported tool chain and IDE which weirds me out.

https://projectlombok.org/features/

Most IDEs nowadays have code generation helpers that make the advantages of using Lombok questionable. Unless you're using some plain text editor (which you shouldn't, even IntelliJ is better than just plain text), there is really not much benefit to it.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.
Yeah I'm not sure its worth having to use a custom toolchain to compile just so you can do @getter

Meanwhile in Eclipse all you do is right click -> encapsulate field and you're done.

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.
We use it for @EqualsAndHashCode almost exclusively, and IMO it's worth it. It is easy to forget to update those methods when you add a new field, and that's an insidious source of bugs that we can flat-out avoid.

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.

Gravity Pike posted:

It is easy to forget to update those methods when you add a new field, and that's an insidious source of bugs that we can flat-out avoid.
This is the main reason we use it as well. It's fine generating a bunch of code when you're first building it, but it's SO EASY to forget stuff when you're adding a field later.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Sagacity posted:

This is the main reason we use it as well. It's fine generating a bunch of code when you're first building it, but it's SO EASY to forget stuff when you're adding a field later.

Just chiming in to say that @Data is the best thing ever, POJOs with none of the boilerplate and free toString, Equals and HashCode, that get updated as I add or remove fields, sign me the gently caress up.

Deep Dish Fuckfest
Sep 6, 2006

Advanced
Computer Touching


Toilet Rascal
I'm looking for a Java book that has both a good depth and breadth in what it covers about the language. Basically, I'm mostly a C++ server programmer who's written bits of Java at various points in the past, but never sat down and learned the language. Ideally it'd be something usable as a reference as much as for learning the details of the language. The equivalent of what I'm looking for for C++ would be "The C++ Programming Language", if that helps any.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

YeOldeButchere posted:

I'm looking for a Java book that has both a good depth and breadth in what it covers about the language. Basically, I'm mostly a C++ server programmer who's written bits of Java at various points in the past, but never sat down and learned the language. Ideally it'd be something usable as a reference as much as for learning the details of the language. The equivalent of what I'm looking for for C++ would be "The C++ Programming Language", if that helps any.

This kinda thing gets asked a lot without any real answer. Most java books should be fine if that's what you really want?

Mostly we just tell people to check out the official java tutorials

Fergus Mac Roich posted:

Oracle's Java tutorials are an excellent place to start.

Sounds weak but check it out, they're pretty in-depth and give you a great explanation and jumping-off point. Maybe work through those and then order something off amazon with good reviews if you need more after you're done or if you get stuck or something.

casual poster
Jun 29, 2009

So casual.

YeOldeButchere posted:

I'm looking for a Java book that has both a good depth and breadth in what it covers about the language. Basically, I'm mostly a C++ server programmer who's written bits of Java at various points in the past, but never sat down and learned the language. Ideally it'd be something usable as a reference as much as for learning the details of the language. The equivalent of what I'm looking for for C++ would be "The C++ Programming Language", if that helps any.

Check out my last post

speng31b
May 8, 2010

YeOldeButchere posted:

I'm looking for a Java book that has both a good depth and breadth in what it covers about the language. Basically, I'm mostly a C++ server programmer who's written bits of Java at various points in the past, but never sat down and learned the language. Ideally it'd be something usable as a reference as much as for learning the details of the language. The equivalent of what I'm looking for for C++ would be "The C++ Programming Language", if that helps any.

Sorry but the Oracle tutorials aren't the equivalent of "The C++ Programming Language", the book you're looking for is Effective Java (http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683) - end of story. The books others mentioned are probably fine books, but not the more indepth look at the language mechanics for someone who knows the language but not the details of how to use it most effectively.

That said, if you don't already know the material in the Oracle tutorials offhand, you should probably at least skim those before bothering with Effective Java, or have them available to reference when you read it.

speng31b fucked around with this message at 01:19 on Feb 24, 2016

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.
Effective Java is great. Java Concurrency in Practice focuses mainly on concurrency issues, but is also a great style and pattern primer as well.

ExcessBLarg!
Sep 1, 2001

Volguus posted:

Most IDEs nowadays have code generation helpers that make the advantages of using Lombok questionable.
If you treat code as write-only, sure. But reducing boiler plate is really helpful for code review, where, in my experience IDE autogenerated code gets blatted wherever and isn't consistently formatted (despite auto-formatters).

speng31b
May 8, 2010

Code generation for boilerplate can be great, but IDE auto generation is also useful. My own rule of thumb is that the project needs to be mature enough to have gone through at least a few major refactors (of at least a some components) before dropping in code generation for that stuff. Prove it's a problem before solving it. Good rule for all sorts of dependencies.

speng31b fucked around with this message at 04:52 on Feb 24, 2016

smackfu
Jun 7, 2004

To tie the two subjects together, Effective Java is really good at explaining where all that boilerplate code comes from, and why it takes 20 lines to write a proper equals or hashcode method.

Too bad it only goes up to Java 6 so I imagine some of the advice is not current best practices. Also IIRC some of the suggestions don't play well with unit testing, like singletons.

Adbot
ADBOT LOVES YOU

speng31b
May 8, 2010

"Never use singletons" is dogmatic and silly. The book teaches you how to use them well if you're using them; it's up to you to know whether you should use them or not. Usually not.

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