What is static in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is static in java?

And how to solve this https://code.sololearn.com/c06H1qThrbU7/?ref=app

8th Feb 2021, 3:31 PM
Aman sharma
15 Answers
+ 5
static method is a class method which is diffenet to a instance method, there both still inside of the class template but a instance method needs to be accessed through an instance of that class were as a static method can be refreneced using just the class name; //1) static method access ..... ClassName.getStaticMethod(); //2) object method access ..... new ClassName().method(); //3) also rember that each object also has access to the static method...... new ClassName().getStaticMethod() example 1 & 3 access the same static method
8th Feb 2021, 4:40 PM
D_Stark
D_Stark - avatar
+ 7
static means that thing doesn't belong to any object, but the whole class. You don't call them by an object, but the class name. For example, pow() is static which is why it is called with Math class. nextDouble() is not static, which is why you need a scanner object to call it.
8th Feb 2021, 3:40 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 4
Yes, void when a function does return nothing. Although you can continue using "return" to break the flow
8th Feb 2021, 3:59 PM
David Ordás
David Ordás - avatar
+ 4
CarrieForle , ChillPill , David Ordás 🇪🇸 , D_Stark Thank you all, you all cleared a big concept of OOP to me. Sorry for consuming your time, I wish I could make a habbit to check in search bar before asking, I always forget that 😅 Thank you all 💕
8th Feb 2021, 5:22 PM
Aman sharma
+ 3
Thnx once again CarrieForle , made my concept of static keyword clear, Can you tell me when we use void with a function?? When it has no return type so casually we write void or anything else, please help
8th Feb 2021, 3:43 PM
Aman sharma
+ 3
CarrieForle I did in simple way but now it's more complicated 😗 help me
8th Feb 2021, 3:51 PM
Aman sharma
+ 3
David Ordás 🇪🇸 thanks, return to be included in function declaration in place of void?
8th Feb 2021, 4:55 PM
Aman sharma
+ 3
Aman sharma With an example better 😉😉 public static void sayHello(String name) { if (name == null) return; System.out.format("Hey sololearner %s!!%n", name); } public static String toText (int option) { if (option == 1) return "Challenge me!"; if (option == 2) return "Thumb up if like my codebits"; .... twrow new IllegalArgumentException(String.format("Unknown option: %s%n")); } This software pattern are called "guard conditionals"
9th Feb 2021, 1:26 AM
David Ordás
David Ordás - avatar
+ 2
David Ordás 🇪🇸 It was really helpful 😁
9th Feb 2021, 1:59 AM
Aman sharma
+ 2
Static: when variables are declared static they are the single variable objects of that class will shared . For example if I create the following code. public class my class { static int num = 5; public static void myMethod(){ System.out.print("hello world"); } } The static variable num with value 5 will be shared among different objects of this class, They can't have different value for num. Also the static method will b called inside the class without creating any object of the class and it will also be used outside the class through its class name not by creating its objects. Examples of such in built static method in Java are Math class methods like sqr, ceil, floor, etc.
9th Feb 2021, 6:10 PM
Ibidunni Ridwan Ayodele
Ibidunni Ridwan Ayodele - avatar
+ 1
Thanks Math class was relatable I had used it so it made me much more clear. If I am not wrong 😁 is String and character also a static class as string has many functions and character too as Character.isDigit()
9th Feb 2021, 6:17 PM
Aman sharma
+ 1
static: method can be run without creating an instance of the class containing the main method
9th Feb 2021, 10:49 PM
Sandy Irvanda
Sandy Irvanda - avatar
10th Feb 2021, 12:06 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
10th Feb 2021, 1:28 AM
Aman sharma
+ 1
Most welcome 👍👍 Keep coding
10th Feb 2021, 1:32 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar