You guessed it,more schoolwork...

I'm not doing very well with not overlapping my methods, I need he

Page 1 of 1

8 Replies - 844 Views - Last Post: 04 March 2010 - 06:00 PM Rate Topic: -----

#1 kweef19  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 19-January 10

You guessed it,more schoolwork...

Posted 04 March 2010 - 03:24 PM

Now we're learning about nesting methods in java programming. I understand about nesting objects in vb.net, but with java its just not as simple to me...I think I botched this code...anyone care to help out?
public class DemoBlock
{
	public static void main(String[] args)
	System.out.println("Demonstrating block scope");
	int x = 1111;
	System.out.println("In first block x is " + x);
	
}
{
	int x = 1111;
	int y = 2222;
	System.out.println("In second block x is " + x);
	System.out.println("In second block y is " + y);
}
{
	int y = 3333;
	System.out.println("In third block x is " + x);
	System.out.println("In third block y is " + y);
	demoMethod();
	System.out.println("After method x is " + x);
	System.out.println("After method block y is " + y);
}	
{
	System.out.println("At the end x is " + x);
	
public static void demoMethod()
{
	int x = 8888, y = 9999;
	System.out.println("In demoMethod x is " + x);
	System.out.println("In demoMethod block y is " + y);
}
}



"I am receiving several errors such as "illegal start of type" and "identifier expected"

This post has been edited by kweef19: 04 March 2010 - 03:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: You guessed it,more schoolwork...

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: You guessed it,more schoolwork...

Posted 04 March 2010 - 03:26 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:


  • Post the code you're having problems with (DONE)
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10669
  • View blog
  • Posts: 39,631
  • Joined: 27-December 08

Re: You guessed it,more schoolwork...

Posted 04 March 2010 - 03:38 PM

View Postkweef19, on 04 March 2010 - 06:24 PM, said:

Now we're learning about nesting methods in java programming. I understand about nesting objects in vb.net, but with java its just not as simple to me...I think I botched this code...anyone care to help out?


Nesting methods? Do you mean having multiple methods in the same class? If so, that should be pretty easy. Below is an example. Note that any code relating to logic flow must be contained in methods.
class MyClass{
   public MyClass(){}

   public int returnOne(){return 1;}

   public String doStuff(){..code..}
}




As for your code, I've made a few comments and changes:
public class DemoBlock
{
         //added a curly brace to the beginning of main()
         //all non-abstract methods use curly braces to 
         //encapsulate their code
	public static void main(String[] args){ 
	System.out.println("Demonstrating block scope");
	int x = 1111;
	System.out.println("In first block x is " + x);
	
    }//end main
{ //rogue brace
	int x = 1111;
	int y = 2222;
	System.out.println("In second block x is " + x);
	System.out.println("In second block y is " + y);
} //rogue brace
{ //rogue brace
	int y = 3333;
	System.out.println("In third block x is " + x);
	System.out.println("In third block y is " + y);
	demoMethod();
	System.out.println("After method x is " + x);
	System.out.println("After method block y is " + y);
}	//rogue brace
{ //rogue brace

          //code that will give you an error and probably
          //prevent compilation, as it needs to be contained
         //in a method
	System.out.println("At the end x is " + x);
	
//all of this code from here down is ok
public static void demoMethod()
{
	int x = 8888, y = 9999;
	System.out.println("In demoMethod x is " + x);
	System.out.println("In demoMethod block y is " + y);
}
}



Note that if I did not add the curly brace at main(), you would have gotten a ton of Class, Interface, or Enum expected errors as well as an identifier expected error.
Was This Post Helpful? 0
  • +
  • -

#4 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: You guessed it,more schoolwork...

Posted 04 March 2010 - 03:39 PM

Your brackets are off. You use brackets with classes, methods, initializer lists for arrays, and some other things as well, not spread out randomly in your code:
class SomeClass {
    public void someMethod(int someParam) {
         //method body
    }//end someMethod
}//end SomeClass


Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: You guessed it,more schoolwork...

Posted 04 March 2010 - 03:44 PM

You can't 'nest methods' in Java. You simply can't have one method inside another. You have braces all over the place, none of which are actually contributing to anything. Remember, all method implementation has to have braces around it.

public class DemoBlock
{       // Need a { here because it's a method.
        public static void main(String[] args) {
            System.out.println("Demonstrating block scope");
            int x = 1111;
            System.out.println("In first block x is " + x);
        // You end you method here (Before the change, it ended the class. 
        //} 
        //{ unnecessary
            int x = 1111;
            int y = 2222;
            System.out.println("In second block x is " + x);
            System.out.println("In second block y is " + y);
        //}
        //{ again... NO idea what you are thinking...
        int y = 3333;
        System.out.println("In third block x is " + x);
        System.out.println("In third block y is " + y);
        demoMethod();
        System.out.println("After method x is " + x);
        System.out.println("After method block y is " + y);
      //}       
      //{ CAN'T JUST HAVE RANDOM BLOCKS!
        System.out.println("At the end x is " + x);
     } // Added to end main() method
        
    // Good.
    public static void demoMethod()
    {
        int x = 8888, y = 9999;
        System.out.println("In demoMethod x is " + x);
        System.out.println("In demoMethod block y is " + y);
    }
}



So... in summary:
1. Methods have to have braces around them.
public void method() {
    //... All code in here.
}



2. There cannot be just "Random" blocks of code flying around.
3. All code that does something HAS to be inside of methods, only variable declarations and methods can be put outside.


Also, I wrote a tutorial on scope and static variables |HERE| if you are interested in learning more.

edit: ninja'd by macosxnerd101 and erik.price :gunsmilie:

edit2: my 2100th post !!! :rockon: :punk: :taz:

This post has been edited by Dogstopper: 04 March 2010 - 03:48 PM

Was This Post Helpful? 1
  • +
  • -

#6 kweef19  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 19-January 10

Re: You guessed it,more schoolwork...

Posted 04 March 2010 - 05:20 PM

I'm pretty sure the point of the assignment was to show that you can have multiple blocks of code within one class. I apologize for such convoluted code. I am still very much a beginner when it comes to java, but I am doing my very best to wrap my mind around it so that I can understand.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10669
  • View blog
  • Posts: 39,631
  • Joined: 27-December 08

Re: You guessed it,more schoolwork...

Posted 04 March 2010 - 05:24 PM

No need to apologize for your code! We are happy to help, and the important thing is that you learn from your mistakes! :)
Was This Post Helpful? 0
  • +
  • -

#8 kweef19  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 19-January 10

Re: You guessed it,more schoolwork...

Posted 04 March 2010 - 05:34 PM

The way you fixed it dogstopper is one way it could be written but I think this exercise is supposed to illustrate the fact that some variables lose scope after they leave the method and then return back to original value in the class.
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: You guessed it,more schoolwork...

Posted 04 March 2010 - 06:00 PM

Then in that case, I suggest reading through the tutorial that I provided. I have several examples of this within the tutorial
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1