+ 1
What is static in java?
And how to solve this https://code.sololearn.com/c06H1qThrbU7/?ref=app
15 Respuestas
+ 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
+ 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.
+ 4
Yes, void when a function does return nothing. Although you can continue using "return" to break the flow
+ 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 💕
+ 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
+ 3
CarrieForle I did in simple way but now it's more complicated 😗 help me
+ 3
David Ordás 🇪🇸 thanks, return to be included in function declaration in place of void?
+ 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"
+ 2
David Ordás 🇪🇸 It was really helpful 😁
+ 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.
+ 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()
+ 1
static: method can be run without creating an instance of the class containing the main method
+ 1
+ 1
Most welcome 👍👍
Keep coding