why do most people use brackets in this way when it comes to Java. Pospular IDEs also use this method by default

class Test{

public void test(){

}

}

than this way

class Test
{

 public void test()
 {

 }

}

The second method is obviously easier to read.

Agreed. Some prefer the first to have more visible lines within a single screen.

Code styles are like religion, they'll trigger holy wars.

commented: Absolutely true about religion! +6

why do most people use brackets in this way when it comes to Java.

Because that's what the official style guide recommends.

The second method is obviously easier to read.

That's not obvious at all. I imagine that most people will find the style easier to read that they are used to.

"obviously easier to read"?

Highly debatable. As long as you're consistent, there's no reason either style isn't readable.
And the K&R style adopted by James Gosling as the recommended standard for Java is arguably clearer to indicate that a block is related to the statement that starts it rather than being somehow independent of it.

The second method is obviously easier to read.

Provided blocks are indented correctly and at a reasonable depth, the location of braces doesn't really matter...to a certain extent. I think you'll find that your preference varies over time. Right now you clearly prefer Allman bracing over K&R, but that might change.

There is no right or wrong way in how you place those brackets.
It's your code. You do it in whatever way feels best for you.
Other java developers who might read your code will understand it just fine.

. You do it in whatever way feels best for you.

wrong. You do it in the way that's used in the remainder of the code base you're working on, according to the coding standards of the project you're working on or the customer you're working for.

And you'll find that most of those will be derived from the official Sun/Oracle coding standards postulated in 1997 (or maybe 1996) and since revived a bit to account for language changes, which are themselves derived from the K&R standards for C.

Maybe I'm the exception, but I honestly have a hard time following code with opening curly braces not on their own line. Luckily I code solo.

For me it's either way.
I usually just go with whatever the developer tool do when I hit enter when creating a method or class.
If the bracket moves to a new line, so be it. The compiler will not ever complain, so why should a person?
If someone tells me I'm doing it wrong, I just simply ignore him/her and do it my way anyway.
On the other hand, if you happen to be part of a coding team you should absolutely follow the coding guidelines set forth by the team.

The compiler will not ever complain, so why should a person?

That's not very sound reasoning. A compiler will also not complain when you name all your variables variable1, variable2 etc., but I can still think of plenty reasons why you shouldn't do that.

Dani, strangely enough it depends on the language for me.
Writing C++ I can't stand having it in K&R layout, writing Java or C in can't stand having it in C++/Stroustrup layout.

I wasn't trying to make a sound reason.
It was just me pointing out a certain point of view.

If you are the only coder, and whoever you do the coding for is only interested in the finished product, then why would anyone but the coder care how you use the curly braces?

Just because some dude in some dusty old office cook up some kind of directive, you are not really obligated to neither read it nor conform to it other than to use the general syntax of the programming language in question.

But that's just my opinion, no need to jump down my throat about it.

Oxiegen:

If you are the only coder, and whoever you do the coding for is only interested in the finished product, then why would anyone but the coder care how you use the curly braces?

because you'll never be "the only coder". usually employers don't expect that, the moment you find a better job, or, if you truly are a righteous loyal dude, retire, they'll have to rewrite every piece of code you've ever written for them.

they'll want to be able to control your work (have another developer audit it), they'll want to be able to bring in more developers (increase spead), so they'll have to be able to read the code. they surely will not want to pause all development each time you go on a holiday, or have to take a sick day.

always write your code as clear as possible, and, according to ruling conventions. it'll make code easier to read, and easier for people to jump in.

Like I said.
Don't jump down my throat just because I have an opinion as an answer to the OP.

stultuske:
Your point of view is based on someone who works as a coder at some company.
My point of view is not.

so your point is: if I write it, it'll never be updated ?
weird philosophy.

My preference is to have the open curly brace on the opening line with starting code. Uses less white space and also I only indent 2 spaces with a tab. I write all comments outside of the function so as not to sloppy the code. I don't do in line commenting.

stultuske:

so your point is: if I write it, it'll never be updated ?
weird philosophy.

That's what you took away from it, that the code would never be updated?
It's your code, you can update it however much you like, or not.

But what does any of this back and forth have to do with where you put the starting curly brace?
And no matter where you put that one single little thing, coders that might view the code after you WILL understand what's going on. And no recoding has to be done, or cleaned up, just for that.
It doesn't matter if some dusty old directive dictates where the curly brace goes. It's readable and can be understood by anyone doing Java.

That was my point. EOF.

U can read the Java Code Conventions in the section 6.2 . The first way are recommended.
Java Code Conventions

(It's quite curious to see the word "recommended" all the time. Meaning that it's not required.
Also, section 6.2 in the convention clearly states: "...at the beginning of blocks". Which is exactly the same as either on the same line as the method declaration or on it's own line. Just because an example shows one thing, doesn't mean that's how it's done.)

Sun/Oracle never enforce it. But that doesn't mean most projects don't enforce it, or something like it.

And most do. So you'd better get used to it or you're not going to last long outside your bedroom.

jwenting:
This, I can agree on. :D

This, I can agree on. :D

jwenting:
This, I can agree on. :D

I prefer the first style :) the second style increases the lines of code.
Also the second style makes it feel like the first line is separate from the ones that come after it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.