Found 7442 Articles for Java

Sleeping for a while in Java

Samual Sam
Updated on 19-Jun-2020 12:04:25

829 Views

You can sleep for any period of time from one millisecond up to the lifetime of your computer. For example, the following program would sleep for 3 seconds −Example Live Demoimport java.util.*; public class SleepDemo {    public static void main(String args[]) {       try {                    System.out.println(new Date( ) + "");                    Thread.sleep(5*60*10);                    System.out.println(new Date( ) + "");               } catch (Exception e) {          System.out.println("Got an exception!");             }    } }This will produce the following result −OutputSun May 03 18:04:41 GMT 2009 Sun May 03 18:04:51 GMT 2009

Create Toast Message in Java Swing

karthikeya Boyini
Updated on 19-Jun-2020 12:08:08

1K+ Views

A toast message is an alert which disappears with time automatically. With JDK 7, we can create a toast message similar to an alert on android very easily. Following are the steps needed to make a toast message.Make a rounded rectangle shaped frame. Add a component listener to frame and override the componentResized() to change the shape of the frame. This method recalculates the shape of the frame correctly whenever the window size is changed.frame.addComponentListener(new ComponentAdapter() {    @Override    public void componentResized(ComponentEvent e) {       frame.setShape(new  RoundRectangle2D.Double(0, 0, frame.getWidth(),       frame.getHeight(), 20, 20));    } ... Read More

Create Toast Message in Java Swing

karthikeya Boyini
Updated on 19-Jun-2020 12:08:08

1K+ Views

A toast message is an alert which disappears with time automatically. With JDK 7, we can create a toast message similar to an alert on android very easily. Following are the steps needed to make a toast message.Make a rounded rectangle shaped frame. Add a component listener to frame and override the componentResized() to change the shape of the frame. This method recalculates the shape of the frame correctly whenever the window size is changed.frame.addComponentListener(new ComponentAdapter() {    @Override    public void componentResized(ComponentEvent e) {       frame.setShape(new  RoundRectangle2D.Double(0, 0, frame.getWidth(),       frame.getHeight(), 20, 20));    } ... Read More

Create translucent windows in Java Swing

Samual Sam
Updated on 19-Jun-2020 12:10:44

411 Views

With JDK 7, we can create a translucent window using swing very easily. With following code, a JFrame can be made translucent.// Set the window to 55% opaque (45% translucent). frame.setOpacity(0.55f);ExampleSee the example below of a window with 55% translucency.import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class Tester {    public static void main(String[] args)  {                     JFrame.setDefaultLookAndFeelDecorated(true);          // Create the GUI on the event-dispatching thread          SwingUtilities.invokeLater(new Runnable() {          @Override          public void ... Read More

Create translucent windows in Java Swing

Samual Sam
Updated on 19-Jun-2020 12:10:44

411 Views

With JDK 7, we can create a translucent window using swing very easily. With following code, a JFrame can be made translucent.// Set the window to 55% opaque (45% translucent). frame.setOpacity(0.55f);ExampleSee the example below of a window with 55% translucency.import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class Tester {    public static void main(String[] args)  {                     JFrame.setDefaultLookAndFeelDecorated(true);          // Create the GUI on the event-dispatching thread          SwingUtilities.invokeLater(new Runnable() {          @Override          public void ... Read More

Are static local variables allowed in Java?

karthikeya Boyini
Updated on 18-Jun-2020 15:41:40

1K+ Views

Unlike C, C++, Java does not allow static local variables. The compiler will throw the compilation error.ExampleCreate a java class named Tester.Tester.javaLive Demopublic class Tester {    public static void main(String args[]) {       static int a = 10;    } }OutputCompile and Run the file to verify the result.Tester.java:3: error: illegal start of expression                    static int a = 10;

Are static local variables allowed in Java?

karthikeya Boyini
Updated on 18-Jun-2020 15:41:40

1K+ Views

Unlike C, C++, Java does not allow static local variables. The compiler will throw the compilation error.ExampleCreate a java class named Tester.Tester.javaLive Demopublic class Tester {    public static void main(String args[]) {       static int a = 10;    } }OutputCompile and Run the file to verify the result.Tester.java:3: error: illegal start of expression                    static int a = 10;

Assigning values to static final variables in java

Samual Sam
Updated on 18-Jun-2020 15:45:03

933 Views

In java, a non-static final variable can be assigned a value at two places.At the time of declaration.In constructor.ExampleLive Demopublic class Tester {    final int A;    //Scenario 1: assignment at time of declaration    final int B = 2;    public Tester() {       //Scenario 2: assignment in constructor       A = 1;    }    public void display() {       System.out.println(A + ", " + B);    }    public static void main(String[] args) {               Tester tester = new Tester();   ... Read More

Assigning values to static final variables in java

Samual Sam
Updated on 18-Jun-2020 15:45:03

933 Views

In java, a non-static final variable can be assigned a value at two places.At the time of declaration.In constructor.ExampleLive Demopublic class Tester {    final int A;    //Scenario 1: assignment at time of declaration    final int B = 2;    public Tester() {       //Scenario 2: assignment in constructor       A = 1;    }    public void display() {       System.out.println(A + ", " + B);    }    public static void main(String[] args) {               Tester tester = new Tester();   ... Read More

Can we overload or override a static method in Java?

Shriansh Kumar
Updated on 16-Apr-2025 19:38:32

4K+ Views

In Java, you cannot override static methods but you can overload them. Overloading a static method is allowed because the JVM determines which method to call at compile time based on the method signature, rather than the object's type. However, overriding is not allowed for static methods because an overridden method is called based on the object's type. In this article, we are going to discuss is it possible to overload and override a static method in Java. Also, we will understand static methods, method overloading and method overriding. What is a Static Method? A method defined using the static ... Read More

Advertisements