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

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.

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.

Step over not working as expected in Eclipse Photon

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.

Static block gets skipped basing on Main method

From the below program I have observed that
main() method must and should not required to be only in next level of main class
All Static blocks are not loaded first.
I would like to know why the static blocks are not loaded?
public class FunnyProgram {
static {
System.out.println("Loading first static block");
}
static class Inner {
static {
System.out.println("Loading first Inner static block first");
}
static class AgainInner {
static {
System.out.println("Loading second AgainInner static class first");
}
public static void main(String[] args) {
System.out.println("This funny program prints");
}
}
}
}

How can I refer to a variable under a static method in the main method?

My codes are like the following.
public class readfile {
public static void readfile() {
int i = 0;
System.out.println("hello");
}
public static void main(String[] args) {
readfile();
System.out.println(i);
}
}
And it works well if I do not refer to the variable i.
(That means it can print out hello.)
So how can I refer i in the main method?
public class readfile {
static int i;
public static void readfile() {
i = 0;
System.out.println("hello");
}
public static void main(String[] args) {
readfile();
System.out.println(i);
}
}
As UUIIUI says in comment, if you declare i inside of readfile(), it is valid only inside of the method.
As Murli says in comment, you need a member variable in the class field. And also it must be static one.
You are writing java code in a bad way :
1.First, the class name char in Java is Uppercase so your class need to be named ReadFile.
You cannot use the i variable in your main method, because it's just a local variable of your readFile method, and you have compilation errors, because of the use of i in the main method, and a warning in the readFile method, because you don't use it in the local block code.
Java appear new for you? and you need to learn a litle bit more. There's a full of book or documentation on the web.
Your sample corrected, compiled well and run well :
package stackWeb;
public class ReadFile {
static int i = 0;
public static void readfile() {
System.out.println("hello");
}
public static void main(String[] args) {
readfile();
System.out.println(i);
}
}
You could try this
class readfile {
static int i =0;
public static void readfile() {
System.out.println("hello");
}
public static void main(String[] args) {
readfile();
System.out.println(i);
}
}

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()
}

Categories