Doubt in java programming.
class cHere we have to declare static void meth()
{
static int x;
public static void meth()//must be static
{
System.out.println("x= "+x);
}
}
public class r
{
public static void main(String args[])
{
c t=new c();
c.meth();
}
}
But look at this one
public class rhere it's not necessary to declare meth as static anybody tell me the reason.
{
int p;
public void meth(int k)
{
p=k;
System.out.println("p= "+p);
}
public static void main(String args[])
{
r t=new r();
t.meth(5);
}
}
😁
Replies
-
sushant005
In the first program u have declare main() method in class r and the meth() is define in class c.So the main() is static and the concept of static is that u can not call a non static methods from is method which is defined as static.That is the reason why you have to define meth() as static in order to access/call it from static method(here main()).The_Small_kHere we have to declare static void meth()
But look at this one
here it's not necessary to declare meth as static anybody tell me the reason.
😁
In the second program you have one class under which you have define both the methods main() and meth() are defined in a single class so the meth() may be declared as static may not be..........
do me correct if i am wrong............ -
Morningdot HabluHey sushant up to what i know if you declare any static method you must have to declare all the members as static.
If any problem check out this
public class r
So how can you tell that
{
static int x=2,z=4;
int y;//this will show an error because this is not static.So you have to declare all the members as static under any static method.
r(int i)
{
y=i;
}
public static void meth()
{
z=x*y;
System.out.println("z= "+z);
}
public static void main(String args[])
{
r t=new r(5);
r.meth();
}
}
main function is static so under that all the members must be static so that's the question dude that here in 1st program you have to declare meth() function as static while 2nd program run without declaring meth() function as static.sushant005so the meth() may be declared as static may not be..........
.
Hope now you clear about the question. -
anandkumarjhahello everyone,
can anyone tell me the uses of final in java? how can it be used to represent variables and methods????/ -
Morningdot Hablu@anand we may have final variable,final data member,final class and also the final parameters.
once you declare a variable as final it content can not be changed.Where as variable may be of simple type or object handled type.
you can declare any variable as final by preceding a final keyword before them.
like final int y;//so here once y is assigned you can not able to modify them.
One example of final variable is
public class r
Once a method is declared final it will lost his overridden properties.
{
public static void main(String args[])
{
final int x=3;
int y=1;
x=y;//doing it will show an error because a final variable content are not alowed to be changed.
System.out.println("x= "+x);
System.out.println("y= "+y);
}
}
Once a class is declared as final all the methods under it will be automatically finalised. -
anandkumarjhaMohit you told that once a method is declared final it will lost his overrriden properties. here what it means by overriden property?could you please explain it for me?
-
Manish Goyalbro I think you are a beginner in java or any other language
I would suggest you pick any book and make your concepts clear ,i am not saying that you should not ask your questions here but from book you can understand more and more
rest is your choice
For time being
I will explain you with an example
say you create a class which which has one method to calculate area of square after some time
say you create a another class in which you want to create a calculate area of circle now obviously you will like to inherit method of base class ,if all the parameters are same ,so you will override this method and create a new method with same name area() but with different functionality ie to calculate area of circle.
Here comes the use of final
if you declare the base class as final then you cannot override the area method with same name
PS:- final is similar to declare something as constant ie it can't change its value once declared as final
correct me if i am wrong -
sushant005
It means that once a class is declared as final then all the methods and members inside that class become final and that class cannot be inherited .anandkumarjhaMohit you told that once a method is declared final it will lost his overrriden properties. here what it means by overriden property?could you please explain it for me? -
anandkumarjhahello everyone
can anyone tell me how superclass variable can reference a subclass object? -
anandkumarjhaAs we know that the subclass inherites all the variables and methods which are publicly declared inside the superclass then what is the need of usin super keyword?
-
Morningdot Hablu
hello anand,anandkumarjhaAs we know that the subclass inherites all the variables and methods which are publicly declared inside the superclass then what is the need of usin super keyword?
ya you are right but think when you don't have permission to create the object of class like Abstract class. Then what you do.At that point you have to use super keyword.
check this program.
abstract class m
Let me know if any problem is still there.
{
int x,y;
m(int p,int q)
{
x=p;
y=q;
}
abstract public int area();
}
class r extends m
{
r(int p,int q)
{
super(p,q);
}
public int area()//Method overriding
{
System.out.println("area of rectangle is ");
return(x*y);
}
}
class t extends m
{
t(int p,int q)
{
super(p,q);
}
public int area()//Method overriding.
{
System.out.println("area of system is ");
return(x+y);
}
}
public class mohit
{
public static void main(String args[])
{
r u=new r(3,4);
t v=new t(1,7);
System.out.println(u.area());
System.out.println(v.area());
}
} -
anandkumarjhahello Mohit
thnxxx alot buddy for making me understand the use of super.Again you have to take the pain to make me understand how super keyword works as this keyword? -
Morningdot Hablu
The keyword super will be used when referring to the super class of ananandkumarjhahello Mohit
thnxxx alot buddy for making me understand the use of super.Again you have to take the pain to make me understand how super keyword works as this keyword?
object.Where as The keyword this will reference the current class the word appears in.
Hope you clear with this example.
class a
Example of this keyword...
{
public int i;
public a(int x)
{
i=x;
}
public void ptr()
{
System.out.println("i= "+i);
}
}
class c extends a
{
public c(int y)
{
super(y);
}
public void ptr()
{
super.ptr();//here super keyword is used as this keyword.
}
}
public class moh
{
public static void main(String args[])
{
c h=new c(1);
h.ptr();
}
}
public class k
super keyword working is same as this keyword only the difference is that this is used as current class object and super is used as super class object.
{
int x;
public void show(int y)
{
this.x=y;
System.out.println("x= "+x);
}
public static void main(String args[])
{
k m=new k();
m.show(4);
}
} -
anandkumarjhaHello everyone
while going through java abstract classes i found that it prohibits the super classs from declearing its objects rather it's object can have it's reference.so if it does not declare its objects then what's the use of abstract classes? -
anandkumarjhawhat is the use of package in java and why each file has to be saved in package?please make me understand?
-
anandkumarjhahello everyone,
can anyone tell me about the INTERFACE in java and what's the use of it? -
sushant005
Packages in Java is the collection of classes and interfaces.anandkumarjhawhat is the use of package in java and why each file has to be saved in package?please make me understand?
To use a class in Java programs then we have import packages in which that file is packed.
For example in Java the default package is java.lang packages and which provides various classes such as Object class,System class etc..That is why while doing simple program we do not need to declare any package because java.lang package is default package.
so in simple sense when we pack different classes and interfaces in a packet that packet is called as the packages very similar to the header files in c/c++ which is a collection of Pre-defined functions. -
sushant005
interface is simply collection of different data members and methods members.Remember that we can not write any codes for function/methods in interface that code must we written in class which is used to implement that interface.anandkumarjhahello everyone,
can anyone tell me about the INTERFACE in Java and what's the use of it?
interface y
Here in the above code you can see that i have created an interface in which i declare a variable f and a method subt() but did not write any code in for that function.the code for that function is written in class b which implements that interface.
{
int f=8;
public void subt();
}
class B implements y
{
int b=9;
void print()
{
System.out.println("B="+b);
subt();
}
public void subt()
{
int y=4;
int z=f-y;
System.out.println("Z="+z);
}
public static void main(String [] args)
{
B q= new B();
q.print();
}
}
and ya interface is the concept which simulates multiple inheritance in JAVA because java does not support multiple inheritance
I think now you are clear about interfaces in java. -
anandkumarjhathank you sushant for explaining about interfaces and packages. But at the end what is the difference between interface and abstract classes then?please explain
-
Ankita KatdareAn abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. -
Ankita KatdareInterfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a Immovable interface.
Whereas an abstract class defines the core identity of a class and there it is used for objects of the same type.
No fields can be defined in interfaces and here, an abstract class can have fields and constrants defined. -
anandkumarjhaThank you brother for making me understand the difference between interfaces and abstract classes
-
paresh006
yes it will be necessary to declare meth as static because only a static method can access static object...The_Small_kHere we have to declare static void meth()
But look at this one
here it's not necessary to declare meth as static anybody tell me the reason.
😁 -
paresh006
Final modifier represents a single instance of method.it can't be modified or redeclared.it represents an inline method concept...so overriding concept is not allowed...it is generally defined in base class during method declaration.the concept approaches to a static binding concept..redeclaration will return error...here i have an example..anandkumarjhahello everyone,
can anyone tell me the uses of final in java? how can it be used to represent variables and methods????/
class A{
final void show(){
System.out.println("\n BASE CLASS");
}
}
class B extends A{
void show(){ //here it will show error...
System.out.println("\nDERIVED CLASS");
}
}
class override{
public static void main(String[] s){
B b=new B();
b.show();
}
}
Another point to be remembered is that if the base class is defined using final modifier then its properties can't be inherited... -
sudhakarnt44both are correct but in the class members if it is static give the facility to call by its class with its member name also so there is no need to create the object of the class.you just call by its name.
eg: 1st class
c.meth();
and second code if you are written both class in the one file then it is necessory to mention only one class as a public and it should be saved by that class name also. -
anandkumarjhawhile going through one article on java my mind stuck on transient variable.can anyone explain what is all about?
-
anandkumarjhahow we actually take the input from the user in java programming?
-
sushant005
this program will give you an idea about how to take input in java.anandkumarjhahow we actually take the input from the user in java programming?
import java.io.*;
This is the one way to take input.
class P
{
public static void main(String [] args)
{
InputStreamReader e=new InputStreamReader(System.in);
BufferedReader w=new BufferedReader(e);
int b;
System.out.println("Enter the value of b");
b=Integer.parseInt(w.readLine());
System.out.println("Entered value of b is:"+b);
}
} -
anandkumarjhaWhat does command line argument actually mean?please explain anyone?
-
anandkumarjhaAs far as exception handling in java is concerned when we use catch for removing our bugs,if we use multiple catch statement one must keep in mind that exception subclass must come before any of their superclass because a catch statement that uses a superclass will catch exception of that type and so subclass will never reach if it come after its superclass
-
anandkumarjhahello everyone
Can anyone please explain as to why do we use the System.in as an input to InputStreamReader, and the InputStreamReader as an input to BufferedReader? -
sushant005Actually InputStreamReader is used to take input and that is stored in the BufferedReader.
-
anandkumarjha
could you please justify your answer??i actually could not understamd itsushant005Actually InputStreamReader is used to take input and that is stored in the BufferedReader. -
Manish GoyalI will give you an example
say you want to water the plants in your garden
then you will use pipe or bucket
if you use pipe
then consider inputstreamreader as pipe and tap as buffered reader here and water tank is system.in here
I hope it is clear now -
anandkumarjhaAlot of thankx goyal for making me understand in the simplest way
-
anandkumarjhawhat are the differences between throw and throws in Exception handling in java programming? can anyone please explain.
-
sushant005The throw statement terminate the normal flow of control of code within any method and thus the execution of program stops and nearest catch block handle the type of exception throw.
Where as Throws statements are used by the methods to determine the type of exception.It means that if method is expecting any type of exception by calling them then exception must be thrown by that method.The throws list the possible type of exception thrown by methods. -
anandkumarjha
Thank you sushant but i have one more doubt that while using throw statement why we use new operator?can you please help me with that?sushant005The throw statement terminate the normal flow of control of code within any method and thus the execution of program stops and nearest catch block handle the type of exception throw.
Where as Throws statements are used by the methods to determine the type of exception.It means that if method is expecting any type of exception by calling them then exception must be thrown by that method.The throws list the possible type of exception thrown by methods. -
Manish Goyal
-
sushant005We need to create an object of an exception class to be thrown by the throw statement so we need new operator to create an object and catch block catch the object and execute the statement through that object.
-
anandkumarjhaI understood the logic of difference between throw and throws clause but one thing peeps into my mind that what actually these clause want to convey when they are used in a single program?
-
anandkumarjhahello every one.......
can we initilize the array size at run time in java programming and if yes then how can we do so? -
anandkumarjhaIf all the thread in java programming language can run without taking consideration of the low priority or high priority regarding time then what's the need of the thread priority in java???
-
Varsha0802In the first example, variable x is a static variable. So, in order to access a static variable, we have to use a static function. whereas in example 2, the variable is not a static variable. We cannot access a static variable inside a non static function.
-
xxxabhash007
Through "Vector" we can create dynamic array which can hold object of any number and of any type.anandkumarjhahello every one.......
can we initilize the array size at run time in java programming and if yes then how can we do so? -
anandkumarjha
can you please illustrate it with one example so as to get it nicelyxxxabhash007Through "Vector" we can create dynamic array which can hold object of any number and of any type. -
anandkumarjhawhat is meant by the synchonized statement in multithreading in java programming
-
ReyaSynchronizing a method is the best way to restrict the use of a method one thread at a time. However, there will be occasions when you won’t be able to synchronize a method, such as when you use a class that is provided to you by a third party. In such cases, you don’t have access to the definition of the class, which prevents you from using the synchronized keyword.
An alternative to using the synchronized keyword is to use the synchronized statement. A synchronized statement contains a synchronized block, within which is placed objects and methods that are to be synchronized. Calls to the methods contained in the synchronized block happen only after the thread enters the monitor of the object.
Although you can call methods within a synchronized block, the method declaration must be made outside a synchronized block. -
anandkumarjhawhat is meant by third party here? can you please explain with one example to make it more easy?
-
ReyaThe synchronized statement synchronizes the instance of the Parentheses class and thus prevents two threads from calling the display() method concurrently.
class Parentheses {
void display(String s) {
System.out.print ("(" + s);
try {
Thread.sleep (1000);
} catch (InterruptedException e) {
System.out.println ("Interrupted");
}
System.out.println(")");
}
}
class MyThread implements Runnable {
String s1;
Parentheses p1;
Thread t;
public MyThread (Parentheses p2, String s2) {
p1= p2;
s1= s2;
t = new Thread(this);
t.start();
}
public void run() {
synchronized(p1){
p1.display(s1);
}
}
}
class Demo{
public static void main (String args[]) {
Parentheses p3 = new Parentheses();
MyThread name1 = new MyThread(p3, "Bob");
MyThread name2 = new MyThread(p3, "Mary");
try {
name1.t.join();
name2.t.join();
} catch (InterruptedException e ) {
System.out.println( "Interrupted");
}
}
}
Here, the display() method is not modified by synchronized. Instead, the synchronized statement is used inside the caller’s run()method. Each thread waits for the prior one to finish before proceeding. -
anandkumarjha@praveena211:thanks a lot dear for your valuable reply.now i got the point and the uses of synchonization....once again thnxxxxx
-
anandkumarjhahello CEans
//to use the values() and valueof()
import java.*;
import java.lang.*;
import java.util.*;
import java.io.*;
enum lodge
{
bed(100),pillow(200),bedseet(50),matress(400);
int price;
lodge(int p)
{
price=p;
}
int show()
{
return price;
}
}
class anand
{
public static void main(String args[])
{
lodge l;
System.out.println("pillow costs"+lodge.pillow.show()+"rupees");
System.out.println("all lodge price");
for(lodge lo:lodge.values());
System.out.println(lo+"costs"+lo.show()+"rupees");
}
}
can anyone please suggest me what's wrong with this coding? when i am compiling it it says some error but i checked it,i didn't found anyone -
Manish GoyalCheck it out
import java.*; import java.lang.*; import java.util.*; import java.io.*; enum lodge { bed(100),pillow(200),bedseet(50),matress(400); int price; lodge(int p) { price=p; } int show() { return price; } } class anand { public static void main(String args[]) { lodge l; lodge[] lol=lodge.values(); System.out.println("pillow costs"+lodge.bed.show()+"rupees"); System.out.println("all lodge price"); for(lodge lo:lol) System.out.println(lo+"costs"+lo.show()+"rupees"); } }
-
anandkumarjhaHello Goyal
it's again showing errors while compiling...... -
Reyahey anand its working perfectly😀check it out properly..
-
anandkumarjhahey Praveena ....
nothing wrong with the coding....i think my jdk has got corrupted....this coding works fine in other lappy -
anandkumarjhaHello CEans
i have installed jdk 1.3 in my laptop.when i am trying to compile enumeration types of java programming it gives errors. Is that due to my old verson of jdk(1.3) or anything else??....should i uninstall my jdk 1.3 ????please suggest -
ReyaInstall jdk1.6 anand..which operating system are you using now?
-
anandkumarjhaI am currently using windows 7 OS.....
Can't i run enumeration type of java programs in jdk 1.3?????? -
anandkumarjhaHello CEans
what are the advantages of wrapper class over primitive data types in java??? -
ReyaWrapper classes allow primitive data types to be accessed as objects. Primitive types are Boolean, Byte, Character, Double, Float, Integer, Long and Short. Wrapper classes make the primitive type data to act as objects.
To deal with primitives as objects is easier at times. But most of the objects collection store objects and not primitive types. Many utility methods are provided by wrapper classes. To get these advantages we need to use wrapper classes. As they are objects, they can be stored in any of the collection and pass this collection as parameters to the methods. -
anandkumarjhaThank you Praveena, i got the idea about wrapper classes
-
anandkumarjhaHello everyone
how Annotation is used in the java programming? -
tripathi.bhaskarfinal keyword is used to prevent overiding of method
it is used to prevent the extensibility of the class
You are reading an archived discussion.
Related Posts
hi am a B.tech 3rd yr student and i have doubt about Quadrant type Electrostatic voltmeters about their construction and principle of working and how exactly the torque is being...
Earth Awards finalists pitch to investors to prove design can build a new economy
18 August 2010: Artificial foam which captures and converts the sun’s energy more effectively than living...
hi,
i wanted to work on voice and speech recognition software using c or c++....can any one assist me to work on it....
i need some help for mini project, may i know what exactly we need to show if we are showing an algorithm for e.g an k-means algorithm via blender animation,...
hi am a 2nd yr electrical student and i want to know wats real power and reactive power.... 😀