Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Structure

  1. #1
    Junior Member Gerardgrundy's Avatar
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question Structure

    Hi I'm doing some code and the use of the Curley braces is confusing me.

    public class a
    {
    private String b;

    public a(String b)
    {
    }

    public String getb()
    {
    return b;
    }
    public void setb(String b)
    {
    this.b = b;
    }

    //but im getting an error in eclipse to
    //add } to complete the class body??

    Am I on the right track with the braces?
    Thanks in advance.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Structure

    Quote Originally Posted by Gerardgrundy View Post
    Hi I'm doing some code and the use of the Curley braces is confusing me.
    Your code with code tags:
    public class a
    {
    	private String b;
     
    public a(String b)	  // your indentation here is not good
    {
    }
     
    	public String getb()
    	{
    	return b;
    	}
    	public void setb(String b)
    	{
    	this.b = b;
    	}
     
    	//but im getting an error in eclipse to 
    	//add } to complete the class body??

    This is easy to figure out yourself. For each opening curly brace, {, the must be a matching closing curly brace, }. If your indentation is good (and yours isn't), it should be readily apparent if things match well. Note that each method should have an opening and matching closing brace and same for every class. I strongly urge you to study how to indent your code correctly so that your code formatting automatically helps you debug your code. Note that Eclipse can do this formatting for you by highlighting the whole class, and then pressing alt-s f for source format.

  3. #3
    Junior Member Gerardgrundy's Avatar
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your quick reply. I gave the alt-s f a go and nothing happens. I've got some custom formatting set up with this quick key. Should the constructor be indented 4 spaces with the rest of the code underneath? Sorry the silly questions but I'm a total beginner.
    Another question.
    Do I need the {} under the constructor statement?
    Regards Gerard

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Structure

    A constructor should be on the same indentation level as a method. Anything inside of a constructor or method should be indented at the next level. The format command in Eclipse won't work if your code doesn't compile, only if it does in fact compile. Whether your starting curly brace is on the same line as your method or constructor signature or on the next line is a matter of style and should be determined by your boss/teacher/coding team.

  5. The Following User Says Thank You to curmudgeon For This Useful Post:

    Gerardgrundy (November 2nd, 2012)

  6. #5
    Junior Member Gerardgrundy's Avatar
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Structure

    public class a
    {
    	private String b;
    	// this is what I indented is this better?
    	public a(String b)	
            {
            }
     
    	public String getb()
    	{
    	return b;
    	}
    	public void setb(String b)
    	{
    	this.b = b;
    	}
    	//I added a curly here so that it compiles
    }

    Is this better?
    Btw how did you get the code to have the colours like in eclipse?

  7. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Structure

    Your braces appear to match, so that's good. As for code format, I'd indent the code within the blocks as well. I like to use 3 spaces (no tabs) since that works well with most forum formatters. For example:

    // class names, A, should begin with a capital letter
    public class A {  // I like this style better -- habit.
       private String b;
     
       public A(String b)	{
          this.b = b;
       }
     
       public String getB() {
          return b;
       }
     
       public void setB(String b) {
          this.b = b;
       }
     
    }

  8. The Following User Says Thank You to curmudgeon For This Useful Post:

    Gerardgrundy (November 3rd, 2012)

  9. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Structure

    Quote Originally Posted by Gerardgrundy View Post
    Btw how did you get the code to have the colours like in eclipse?

    [code=java]
    // your code goes here
    [/code]

    looks like:

    // your code goes here

  10. The Following User Says Thank You to helloworld922 For This Useful Post:

    Gerardgrundy (November 2nd, 2012)

  11. #8
    Junior Member Gerardgrundy's Avatar
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Structure

    public class a
    {
    	private String b;
            private String c
    	// this is what I indented is this better?
    	public a(String b, String c)
    // do i need these curly braces below. why do i need them?	
            {
            }
     // getters and setters
    	public String getB()
    	{
    	return b;
    	}
    	public void setB(String b)
    	{
    	this.b = b;
    	}
            public String getC()
    	{
    	return c;
    	}
    	public void setC(String c)
    	{
    	this.c = c;
    	}
     
    }


    Im adding another variable to the contractor. Why do I need the braces under the constructor line?

  12. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Structure

    Im adding another variable to the contractor. Why do I need the braces under the constructor line?
    The braces are required to mark the code block that belongs to the constructor. Anything contained within the braces will be executed when the constructor is called. For example you could add lines of code to set the variables...

  13. The Following User Says Thank You to jps For This Useful Post:

    Gerardgrundy (November 3rd, 2012)

Similar Threads

  1. [SOLVED] am i using the right structure for applet?
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 23rd, 2012, 11:24 PM
  2. Re: Data Structure
    By jim17 in forum Object Oriented Programming
    Replies: 3
    Last Post: November 16th, 2011, 11:14 PM
  3. HI can some one tell me the size of the below structure.
    By sucheth13 in forum Java Native Interface
    Replies: 1
    Last Post: March 11th, 2011, 03:08 AM

Tags for this Thread