Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
function test ()
{
console.log(a);
console.log(foo());
var a = 1;
function foo ()
{
 return 2;
}
}
test();


What I have tried:

Nothing, because the above code doesn't even compile.
Posted
Updated 8-Oct-17 22:53pm
v2

As it stands, the program you proposed is just (incorrect) pseudo code. You really need, as already suggested, to read a Java tutorial to grasp, at least, the rudimentals of such a programming language.
Below a working Java program, based on your pseudo code.
Java
class FooTest // you need at least a class, in a Java program
{
  int a;

  public FooTest(int a){this.a = a;} // class ctor

  public void test () // this is an 'instance method' of the class
  {
    int a = 1;
    System.out.println(a);
    System.out.println(FooTest.foo()); // note you don't need an instance of the class in order to call the 'foo' static method
  }
  public static int foo () // this is a 'static method' of the class
  {
    return 2;
  }

  public static void main ( String args[]) // the main function is the required program entry point
  {
    FooTest ft = new FooTest(42); // you need an instance of the class in order to call the test method
    ft.test();
  }
}
 
Share this answer
 
am unable to execute the code what is reason
 
Share this answer
 
v2
Comments
Richard MacCutchan 9-Oct-17 3:32am    
That will not even compile so there is no way you are going to be able to execute it. I suggest you get hold of a good book on Java, or try The Java™ Tutorials[^].
Patrice T 9-Oct-17 3:53am    
you will be able to execute the code once you corrected the code so that it compile.
Richard Deeming 10-Oct-17 12:07pm    
In what way is this a "solution" to your question?

And why have you accepted it as the answer?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900