Results 1 to 8 of 8
  1. #1
    Aga^^ is offline Member
    Join Date
    Mar 2009
    Posts
    10
    Rep Power
    0

    Exclamation How can i use a class's variable in another class?

    hi guys i am a newbie on java.
    I am working on a project: i have 3 classes one of them is laptop, second is desktop and the third one is computer for example. How can i use laptop's variable in computer class?
    i m waiting for your answers... thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It's impossible to know how to answer your question without more information. A great resource that has helped me and will probably help you is an article entitled How To Ask Questions The Smart Way. It will tell you how to formulate your questions so that the folks here will be better able to answer them.

    I suspect though given the names of your classes that laptop extends computer, but again it's impossible to know without more information. If this is the case, then you most definitely do not want computer to use any of laptops methods. If this answer doesn't help you, then please read the link above and provide us with more information. The link will tell you what we need to see.

  3. #3
    Aga^^ is offline Member
    Join Date
    Mar 2009
    Posts
    10
    Rep Power
    0

    Default

    Term Class
    Java Code:
    public class Term 
    {	
    	public int upper;  //polinomun ussunu tanimladim
    	public int exponent; //polinomun katsayisi
    	
    	public Term()  //constructor
    	{
    		exponent=0;
    	}
    
    	public void setUpper(int upper)
    	{
    		this.upper=upper;    
    	}
    	
    	public void setExponent(int exponent)
    	{
    		this.exponent=exponent; 
    	}
    	
    	public int getUpper()
    	{
    		return this.upper;
    	}
    	
    	public int getExponent()
    	{
    		return this.exponent;
    	}
    	
    	public Term(int upper,int exponent)
    	{
    		setUpper(upper);
    		setExponent(exponent);
    	}
    	
    	public String toString()
    	{
    	return String.format(
    			"#Polynomial# Exponent : %s, Upper : %s",this.exponent,this.upper);	
    	}	
    }
    Java Code:
    public class Polynomial
    {
    	ArrayList<Term> poli=new ArrayList<Term>();
    	Term term=new Term();
    	
    	public int birinci;
    	public int ikinci;
    	public int ucuncu;
    
    	public int getBirinci()
    	{
    		return this.birinci;
    	}
    	
    	public int getIkinci()
    	{
    		return this.ikinci;
    	}
    	
    	public int getUcuncu()
    	{
    		return this.ucuncu;
    	}
    }
    above the classes are.
    I want to display in the console polynomial's exponent (exponent is a variable of term)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    which Term? The Term variable? or the Term objects within the arraylist? Either way, the Polynomial object can have a toString method that calls either the term's toString method or using a StringBuilder, builds a String by iterating through the items in the poli ArrayList.

  5. #5
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    That depends of the task you want to cope. If you need to init var during the whole program activity time you may use static variable...
    So we have class A and B... We have var in A and want to access var from B to init A... so we must declare var in B as public to make it accessable from A. Or we must declare var as protected... You need to know more of modificators in Java
    First describe your problem in a more specific way :)

  6. #6
    Aga^^ is offline Member
    Join Date
    Mar 2009
    Posts
    10
    Rep Power
    0

    Default

    ok i get it thanks..

  7. #7
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    By the way, it's generally a bad idea to make all member variables public.

  8. #8
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Quote Originally Posted by toadaly View Post
    By the way, it's generally a bad idea to make all member variables public.
    Not all but you need to )

Similar Threads

  1. Calling a variable from a different java class
    By jdiesel in forum New To Java
    Replies: 4
    Last Post: 01-09-2009, 01:12 AM
  2. Interface variable to class
    By zill in forum Advanced Java
    Replies: 6
    Last Post: 10-11-2008, 03:29 AM
  3. Which class and variable to choose?
    By kian_hong2000 in forum New To Java
    Replies: 8
    Last Post: 08-27-2008, 07:06 AM
  4. Object class's equals() method behavior????
    By skyineyes in forum New To Java
    Replies: 4
    Last Post: 07-19-2008, 11:58 PM
  5. Public class variable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •