Results 1 to 9 of 9
  1. #1
    thorbcn is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default use a class in another class

    hi all,
    how to do this best way in java:
    if i have a class:
    class A {
    public String getWebPage()
    {
    }
    }
    then i have a class B where i want to use class A method getWebPage,
    i have to use that method that gets a web page from class A in class B 800 times,
    how to do that
    i define a var in class B:
    public String request = new A();
    and then i use 800 times request.getWebPage();
    it's good idea to do it static in class A,
    which is the best way to use any class method from a class in another java class a lot of times
    thinking about speed, performance.......
    when to use threads.....
    thanks a lot

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    public String request = new A();
    Class A is not a String so this will not compile. But you are on the right track. In class B you need to create an instance of class A, then you can call its methods. Why do you care about speed and performance? The main concern should be to write code that compiles and runs successfully. If you discover that your code takes 3 hours to complete or crashes due to running out of memory then you can revisit your code and see where you can improve it.

  3. #3
    thorbcn is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    hi thanks for your response,
    i have another issue, like i said in a class A i have this method:
    class A {
    public String getWebPage()
    {
    // code to get a remote page url = new URL(page);
    // and the rest of the code to do it
    // it's a string cos the web i get i do reponse.toString();
    return response;
    }
    }
    then in my class B:

    import org.me.classA;
    classB {
    private classA request = new classA();

    i have 2 methods:
    private String method 1() {
    String page1 = request.getWebPage();
    return page1;
    }

    private String method 2() {
    String page2 = request.getWebPage();
    return page2;
    }

    }
    doing this the method2 returns the same page i mean page1 = page2,
    if i change that for this:

    import org.me.classA;
    classB {

    private String method 1() {
    classA request = new classA();
    String page1 = request.getWebPage();
    return page1;
    }

    private String method 2() {
    ClassA request1 = new classA();
    String page2 = request1.getWebPage();
    return page2;
    }

    }

    it works page 1 != page2 i get 2 different pages what i need,
    could you tell me if it's the best way or how can i get the 2 different pages
    with just one object A and calling getWebPage method so many times as i need instead of
    creating a new object A every time i need to get a web page cos then i have to cretae 800
    objects A, or the problem is in class A where i have the method getWebPage
    thanks a lot
    Last edited by thorbcn; 07-24-2011 at 06:03 AM.

  4. #4
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Java Code:
    class A {
    public static String getWebPage()
    {
    // code to get a remote page url = new URL(page);
    // and the rest of the code to do it
    // it's a string cos the web i get i do reponse.toString();
    return response;
    }
    }
    Java Code:
    classB {
    
    private String method 1() {
    String page1 = A.getWebPage();
    return page1;
    }
    
    private String method 2() {
    String page2 = A.getWebPage();
    return page2;
    }
    
    }
    this is wrong
    Java Code:
    classA request = new classA();
    name for your class is A not classA
    Java Code:
    A request = new A();

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by fakepics500 View Post
    Java Code:
    class A {
    public static String getWebPage()
    {
    ...
    return response;
    }
    }
    Why did you make it a static method? The OP didn't say it was static. Static methods are so C-ish and procedural programming practice ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    thorbcn is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    thanks for your responses,
    i did some mistakes posting the message,
    i know this is bad classA request = new classA();
    it's A request = new A();
    just bad posting,
    public static String getWebPage()
    {
    ...
    return response;
    }
    it's not a static method,
    still have the problem i need to create 800 objects A to use class A getWebPage method instead of
    just create one object A and use 800 times getWebPage method cos firt page i get in this way
    is the page i get in the 800 other request

  7. #7
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Java Code:
    class B {
    
    B()
    {A request=new A();
    }
    private String method 1() {
    String page1 = request.getWebPage();
    return page1;
    }
    
    private String method 2() {
    String page2 = request.getWebPage();
    return page2;
    }
    
    }
    in this code your only making one object request which can be used to call getwebpage() 800 times

  8. #8
    thorbcn is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    yes but it does not work like that the second page returns the first page two,
    if i change that for this:
    class B {

    B()
    {
    }
    private String method 1() {
    A request=new A();
    String page1 = request.getWebPage();
    return page1;
    }

    private String method 2() {
    A request2 =new A();
    String page2 = request2.getWebPage();
    return page2;
    }

    }

    then it works i mean if i create 2 objects A and then i use getWebPage() method of class A for every object i get different pages,
    but if i just create one object A and i use 2 times.... the method getWebPage() for the same object A i always get the same page,
    i think i had to get different pages even with one object A created need some help to understand this
    thanks

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by thorbcn View Post
    yes but it does not work like that the second page returns the first page two,
    if i change that for this:
    class B {

    B()
    {
    }
    private String method 1() {
    A request=new A();
    String page1 = request.getWebPage();
    return page1;
    }

    private String method 2() {
    A request2 =new A();
    String page2 = request2.getWebPage();
    return page2;
    }

    }

    then it works i mean if i create 2 objects A and then i use getWebPage() method of class A for every object i get different pages,
    but if i just create one object A and i use 2 times.... the method getWebPage() for the same object A i always get the same page,
    i think i had to get different pages even with one object A created need some help to understand thisat
    thanks
    How does your getWebPage() method 'know' that it has to return a different (String) webpage when called from a different object? The A() constructor doesn't receive any parameter(s), nor the getWebPage() method. I find it an example of suspicious coding with nasty side effects.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 03-27-2011, 06:49 AM
  2. Replies: 5
    Last Post: 01-26-2011, 07:38 PM
  3. Replies: 8
    Last Post: 04-14-2010, 06:49 AM
  4. Replies: 7
    Last Post: 05-06-2009, 01:42 PM
  5. Replies: 8
    Last Post: 03-21-2009, 10:34 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
  •