Doubt in java programming.

class c
{
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();
}
}
Here we have to declare static void meth()
But look at this one
public class r
{
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);
}
}
here it's not necessary to declare meth as static anybody tell me the reason.
😁

Replies

  • sushant005
    sushant005
    The_Small_k
    Here 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 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()).

    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 Hablu
    Morningdot Hablu
    Hey 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
    {
    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();
    }
    }
    So how can you tell that
    sushant005
    so the meth() may be declared as static may not be..........
    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.
    .
    Hope now you clear about the question.
  • anandkumarjha
    anandkumarjha
    hello everyone,
    can anyone tell me the uses of final in java? how can it be used to represent variables and methods????/
  • Morningdot Hablu
    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
    {
    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 method is declared final it will lost his overridden properties.
    Once a class is declared as final all the methods under it will be automatically finalised.
  • anandkumarjha
    anandkumarjha
    Mohit 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 Goyal
    Manish Goyal
    bro 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
    sushant005
    anandkumarjha
    Mohit 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?
    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 .
  • anandkumarjha
    anandkumarjha
    hello everyone
    can anyone tell me how superclass variable can reference a subclass object?
  • anandkumarjha
    anandkumarjha
    As 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
    Morningdot Hablu
    anandkumarjha
    As 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?
    hello anand,
    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
    {
    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());
    }
    }
    Let me know if any problem is still there.
  • anandkumarjha
    anandkumarjha
    hello 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
    Morningdot Hablu
    anandkumarjha
    hello 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?
    The keyword super will be used when referring to the super class of an
    object.Where as The keyword this will reference the current class the word appears in.
    Hope you clear with this example.
    class a
    {
    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();
    }
    }
    Example of this keyword...
    public class k
    {
    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);
    }
    }
    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.
  • anandkumarjha
    anandkumarjha
    Hello 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?
  • anandkumarjha
    anandkumarjha
    what is the use of package in java and why each file has to be saved in package?please make me understand?
  • anandkumarjha
    anandkumarjha
    hello everyone,
    can anyone tell me about the INTERFACE in java and what's the use of it?
  • sushant005
    sushant005
    anandkumarjha
    what is the use of package in java and why each file has to be saved in package?please make me understand?
    Packages in Java is the collection of classes and interfaces.
    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
    sushant005
    anandkumarjha
    hello everyone,
    can anyone tell me about the INTERFACE in Java and what's the use of it?
    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.


    interface y
    {
    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();
    }
    }
    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.

    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.
  • anandkumarjha
    anandkumarjha
    thank 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 Katdare
    Ankita Katdare
    An 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 Katdare
    Ankita Katdare
    Interfaces 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.
  • anandkumarjha
    anandkumarjha
    Thank you brother for making me understand the difference between interfaces and abstract classes
  • paresh006
    paresh006
    The_Small_k
    Here 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.
    😁
    yes it will be necessary to declare meth as static because only a static method can access static object...
  • paresh006
    paresh006
    anandkumarjha
    hello everyone,
    can anyone tell me the uses of final in java? how can it be used to represent variables and methods????/
    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..
    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...
  • sudhakarnt44
    sudhakarnt44
    both 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.
  • anandkumarjha
    anandkumarjha
    while going through one article on java my mind stuck on transient variable.can anyone explain what is all about?
  • anandkumarjha
    anandkumarjha
    how we actually take the input from the user in java programming?
  • sushant005
    sushant005
    anandkumarjha
    how we actually take the input from the user in java programming?
    this program will give you an idea about how to take input in java.
    import java.io.*;
    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);

    }
    }
    This is the one way to take input.
  • anandkumarjha
    anandkumarjha
    What does command line argument actually mean?please explain anyone?
  • anandkumarjha
    anandkumarjha
    As 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
  • anandkumarjha
    anandkumarjha
    hello 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?
  • sushant005
    sushant005
    Actually InputStreamReader is used to take input and that is stored in the BufferedReader.
  • anandkumarjha
    anandkumarjha
    sushant005
    Actually InputStreamReader is used to take input and that is stored in the BufferedReader.
    could you please justify your answer??i actually could not understamd it
  • Manish Goyal
    Manish Goyal
    I 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
  • anandkumarjha
    anandkumarjha
    Alot of thankx goyal for making me understand in the simplest way
  • anandkumarjha
    anandkumarjha
    what are the differences between throw and throws in Exception handling in java programming? can anyone please explain.
  • sushant005
    sushant005
    The 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
    anandkumarjha
    sushant005
    The 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.
    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?
  • sushant005
    sushant005
    We 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.
  • anandkumarjha
    anandkumarjha
    I 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?
  • anandkumarjha
    anandkumarjha
    hello every one.......
    can we initilize the array size at run time in java programming and if yes then how can we do so?
  • anandkumarjha
    anandkumarjha
    If 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???
  • Varsha0802
    Varsha0802
    In 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
    xxxabhash007
    anandkumarjha
    hello every one.......
    can we initilize the array size at run time in java programming and if yes then how can we do so?
    Through "Vector" we can create dynamic array which can hold object of any number and of any type.
  • anandkumarjha
    anandkumarjha
    xxxabhash007
    Through "Vector" we can create dynamic array which can hold object of any number and of any type.
    can you please illustrate it with one example so as to get it nicely
  • anandkumarjha
    anandkumarjha
    what is meant by the synchonized statement in multithreading in java programming
  • Reya
    Reya
    Synchronizing 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.
  • anandkumarjha
    anandkumarjha
    what is meant by third party here? can you please explain with one example to make it more easy?
  • Reya
    Reya
    The 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
    anandkumarjha
    @praveena211:thanks a lot dear for your valuable reply.now i got the point and the uses of synchonization....once again thnxxxxx
  • anandkumarjha
    anandkumarjha
    hello 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 Goyal
    Manish Goyal
    Check 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");
    
    }
    }
  • anandkumarjha
    anandkumarjha
    Hello Goyal
    it's again showing errors while compiling......
  • Reya
    Reya
    hey anand its working perfectly😀check it out properly..
  • anandkumarjha
    anandkumarjha
    hey Praveena ....
    nothing wrong with the coding....i think my jdk has got corrupted....this coding works fine in other lappy
  • anandkumarjha
    anandkumarjha
    Hello 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
  • Reya
    Reya
    Install jdk1.6 anand..which operating system are you using now?
  • anandkumarjha
    anandkumarjha
    I am currently using windows 7 OS.....
    Can't i run enumeration type of java programs in jdk 1.3??????
  • anandkumarjha
    anandkumarjha
    Hello CEans
    what are the advantages of wrapper class over primitive data types in java???
  • Reya
    Reya
    Wrapper 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.
  • anandkumarjha
    anandkumarjha
    Thank you Praveena, i got the idea about wrapper classes
  • anandkumarjha
    anandkumarjha
    Hello everyone
    how Annotation is used in the java programming?
  • tripathi.bhaskar
    tripathi.bhaskar
    final 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.... 😀