Step over not working as expected in Eclipse Photon - java

I have the below code for debugging:
public class DebugTest {
public static void main(String[] args) {
methodOne();
System.out.println("main method");
}
private static void methodOne() {
System.out.println("methodone");
methodTwo();
}
private static void methodTwo() {
methodThree();
}
private static void methodThree() {
System.out.println("methodThree");
}
}
Somehow the step over(f6) at method call methodOne() is not moving to next line but instead stepping into the function call. Am i missing something here?

It's because methodTwo() doesn't exist. It jumps there because you want to jump over a method which causes a problem and instead of jump over it goes to the problem line.
If you replace in methodOne() printing and invoking methodTwo() you can see that it jumps to the line with methodTwo() not print.
public static void main(String[] args) {
methodOne();
System.out.println("main method");
}
private static void methodOne() {
System.out.println("methodone");
methodTwo();
}
If you create methodTwo() it normally jumps over this line.

public static void main(String[] args) {
methodOne();
System.out.println("main method");
}
private static void methodOne() {
System.out.println("methodone");
methodTwo();
}
That's because I didn't implement the second method. Change the output statement and order, implement the second method and debug it.

Related

Unable to call function in java

When I try to call the lyrics function in this code, "invalid method declaration; return type required" occurs.
I'm learning java and very very new to it. I'm confused how to define the function and call the function so that code may run.
public class Main {
public static void main(String[] args) {
}
public void lyrics() {
System.out.println("some lyrics here");
}
lyrics();
}
Normally, one can't just invoke a method randomly in the body of the code. However, there is something called an initialization block (this gets run in the body of the constructors of the object). I think an example might clarify. Like,
public class Main {
public static void main(String[] args) {
new Main(); // <-- instantiate an instance of Main
}
public void lyrics() {
System.out.println("some lyrics here");
}
{ // <-- this is an initialization block
lyrics();
}
}
The above uses the default constructor, we can add an explicit one. Like,
public Main() {
super();
System.out.println("In Main constructor");
}
Note how the output changes.
They can also be static (and run when the class is first referenced). Like,
public class Main {
public static void main(String[] args) {
}
public static void lyrics() {
System.out.println("some lyrics here");
}
static {
lyrics();
}
}
Your code is nearly correct. Your lyrics() method must be static if you want to call it inside main method because main method is static. Non static members cannot be accessed from static method (without creating an instance to invoke it).
public class Main {
public static void main(String[] args) {
lyrics();
}
public static void lyrics() {
System.out.println("some lyrics here");
}
}
You can invoke non-static methods from static method by creating an instance of the class containing non-static method inside your main method as mentioned in comments by Elliott Frisch.
new Main().lyrics();
Since you're learning Java, you must remember that the only way to call a non-static method from a static method is to have an instance of the class containing the non-static method.
Or you can simply make your function static. Also, you need to call the function inside the main block.
public class Main {
public static void main(String[] args)
{
lyrics();
}
public static void lyrics()
{
System.out.println("some lyrics here");
}
}
Happy coding!
The method should be declared as static and function call should be within main method. The correct code should look like as follows:
public class Main {
public static void main(String[] args)
{
lyrics();
}
public static void lyrics()
{
System.out.println("some lyrics here");
}
}
You need to move the call to lyrics() into the main method where code is actually executed. Where you have it now, the compiler is expecting a new method defination.
public class Main {
public static void main(String[] args) {
// execution begins here.
new Main().lyrics();
}
public void lyrics() {
System.out.println("some lyrics here");
}
}
EDIT: created new class Main class to avoid errors.

How to execute a java nippet from another one

I have the following classic java script.
public class HelloWorld3 {
public static void main(String[] args) {
System.out.println("Hello");
}
}
I just want to run this script from another script so that it just prints out "Hello"
this was my attempt to do just that.
public class Test {
public static void main(String[] args) {
HelloWorld3 obj = new HelloWorld3();
System.out.println(obj);
}
}
which failed and I get why it failed.
Not sure how to do it right though.
The main method is used to start a program, there is no need to have it in two different classes and to call one from the other.
To construct an instance of a class you need a constructor. Here's one:
public class HelloWorld3 {
public HelloWorld3(){
}
}
Now you can call new HelloWorld3 from another class (such as Test) to create a HelloWorld3 type object. If you want this object to print a message, lets add a method to it:
public class HelloWorld3 {
public HelloWorld3(){
}
public void printHello(){
System.out.println("Hello");
}
}
You may now use this main method inside your Test class:
public static void main(String[] args) {
HelloWorld3 obj = new HelloWorld3();
obj.printHello();
}
Modify the HelloWorld3
public class HelloWorld3 {
public static void main(String[] args) {
System.out.println("Hello");
}
public void printMeStatic(String msg) {
System.out.println(msg);
}
public void printMeInstace(String msg) {
System.out.println(msg);
}
}
Use it in other class
public static void main(String[] args) {
HelloWorld3 obj = new HelloWorld3();
System.out.println(obj.printMeInstace("a msg 1")); //use this if you need an instance/ object of the class HelloWorld3
System.out.println(HelloWorld3.printMeStatic("a msg 2")); //use this for static methods (you dont need an object to use them)
}

Illegal Start of Expression : declaring a method inside another method

I know this will probably be an easy fix, but i'm just starting out in java. I need to declare a method inside the main method that clears the screen. Line 5 is giving me an error called Illegal start of expression.
public class Project2
{
public static void main(String [] args)
{
public static void clearScreen()
{
System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}// end clearScreen()
System.out.print("\nDid it work?");
}
}
Nested methods is not allowed in Java(as of yet). The closest you can get is
class Project2 {
public static void main(String [] args) {
class InnerClass {
void clearScreen() {
// Do something.
}
}
new InnerClass().clearScreen(); // Call it this way.
}
}
If the above solution doesn't suit, then just move that method outside your main and call it.
You can't put a method inside a method like that. You call methods from methods, like so:
public class Project2
{
public static void main(String [] args)
{
clearScreen();
System.out.print("\nDid it work?");
}
public static void clearScreen()
{
System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}// end clearScreen()
}

I want to print "Hello" even before main() is executed

I want to print "Hello" even before main() is executed in Java Program. Is there any way for doing this?
What you need is a static keyword. One of the options is to use static function as initializer to static variable.
class Main {
public static int value = printHello();
public static int printHello() {
System.out.println("Hello");
return 0;
}
public static void main(String[] args) {
System.out.println("Main started");
}
}
value is a static variable so initialized before main function execution. This program prints:
Hello
Main started
Moreover, you can even simplify this by calling printHello() even without variable initialization like in the following:
static {
printHello();
}
public class Sample {
static {
System.out.println("Hello first statement executed first ");
}
public static void main(String[] args) {
System.out.println("Main started");
}
}
Use a static block:
static {
System.out.println("hello");
}
public static void main(String[]args) {
System.out.println("After hello");
}
Output:
hello
after hello
public class Test {
static {
System.out.println("Hello");
}
public static void main(String[] args) {
System.out.println("Inside Main");
}
}
Outputs
Hello
Inside Main
Print the statement inside a static block of code. Static blocks get executed when the class gets loaded into the memory and even before the creation of an object. Hence it will be executed before the main() method. And it will be executed only once.
Apart from using static block, you can also try instrumentation and premain
http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/package-summary.html
import java.io.*;
class ABCD {
public static int k= printit();
public static int printit(){
System.out.println("Hello it will be printed before MAIN");
return 0;
}
public static void main (String[] args) {
System.out.println("Main method is Executed");
}
}
Static variables are initialized in the start of execution of program . So to initialize it will call printit(); function which will be executed and "Hello it will be printed before MAIN" will be printed and in last function will return value '0' and finally after this main block will be executed.
Final Output :
Hello it willl be printed before MAIN
Main method is Executed
Here is another way:
class One{
public One() {
System.out.println("Before main");
}
}
class Two extends One{
public Two() {
super();
}
public static void main(String[] args) {
Object abj = new Two();
System.out.println("in the main");
}
}
Here in run configuration, class Two would be the main class.

Call an overridden method in my main class

My code is like this...
but there seems to be a problem when I call the overridden method createHome(). Here is a sample code:
public class Test extends SweetHome3D {
public static void main(String [] args) {
new Test().init(args);
***createHome();***
}
#Override
public Home createHome() {
Home home = super.createHome();
// Modify home as you wish here
return home;
}
}
I take it that code didn't compile? You are calling createHome() as if it's a static method.
public static void main(String [] args) {
Test test = new Test();
test.init(args);
test.createHome();
}

Categories